如果能夠使自己程序具有短信發送功能,那將會使您的程序蓬蓽生輝,特別是通訊錄等程序。通常在程序中發送短信有兩種方式,一種是使用GSM Modem通過計算機串口編程發送短信,這種方式需要購置硬件設備,既不方便又不經濟!另一種方式是通過網絡發送,我們可以先在163.com等網站上注冊一個用戶,然后通過這些具有短信發送功能的網站發送短信。這種方式比較經濟。下面本文講述第二種的實現方式。
實際上,我們的程序只要具有象瀏覽器那樣向HTTP服務器發送數據的功能,我們就可以模擬瀏覽器的登陸等操作,通過程序來發送短信。我們打算使用WinInet函數來實現與HTTP服務器的通信。
主要就兩個函數,一個就是模擬瀏覽器的函數,另一個是發送短信的函數,其中使用了網易作為測試樣例,您還可以自己添加其他短信網關。
/*****************************************************************
* 函數介紹: 執行HTTP的Post或Get方法
* 輸入參數: TCHAR* hdrs - HTTP頭
TCHAR* accept - Accept類型
TCHAR* Method - POST 或 GET
TCHAR* frmdata - 要提交的數據
TCHAR* ServerName - 服務器地址
TCHAR* FormAction - 數據提交到的網頁名稱
* 輸出參數: 無
* 返 回 值: int - 返回操作狀態(見SendSMS)
*****************************************************************/
int doHTTP(TCHAR* hdrs, TCHAR* accept, TCHAR* Method, TCHAR* frmdata, TCHAR* ServerName, TCHAR* FormAction)
{
// 創建Internet
HINTERNET hsession = InternetOpen("MyAgent",
INTERNET_OPEN_TYPE_PRECONFIG,
NULL,
NULL,
0);
if (!hSession)
{
return 5;
}
// 連接服務器
HINTERNET hConnect = InternetConnect(hSession,
ServerName,
INTERNET_DEFAULT_HTTP_PORT,
NULL,
NULL,
INTERNET_SERVICE_HTTP,
0,
1);
if (!hConnect)
{
return 2;
}
// 創建一個請求
HINTERNET hRequest = HttpOpenRequest(hConnect,
Method,
FormAction,
HTTP_VERSION,
NULL,
(const char**)&accept,
0,
1);
if (!hRequest)
{
return 2;
}
// 發送請求
BOOL bSendRequest = HttpSendRequest(hRequest,
hdrs,
strlen(hdrs),
frmdata,
strlen(frmdata));
if (!bSendRequest)
{
return 2;
}
////////////////////////調試用/////////////////
#ifdef _DEBUG
int bDoLoop = 1;
LPTSTR szReadBuffer;
DWord lNumberOfBytesRead;
FILE* f1;
szReadBuffer = (LPTSTR) malloc(500);
ZeroMemory(szReadBuffer, 500);
if ((f1=fopen("c://test.htm", "w"))!=NULL)
{
while(bDoLoop)
{
bDoLoop = InternetReadFile(hRequest, szReadBuffer, 500, &lNumberOfBytesRead);
fseek(f1, 0L, SEEK_END);
fwrite(szReadBuffer, sizeof(szReadBuffer), lNumberOfBytesRead, f1);
if (lNumberOfBytesRead<500)
bDoLoop = 0;
}
}
fclose(f1);
free(szReadBuffer);
#endif
//////////////////////////////////////////////////
// 清除句柄
if (hRequest)
InternetCloseHandle(hRequest);
if (hConnect)
InternetCloseHandle(hConnect);
if (hSession)
InternetCloseHandle(hSession);
return 0;
}
/*****************************************************************
* 函數介紹: 發送短信函數
* 輸入參數: char* lpGateway - 發送網關名稱
char* lpUserName - 發送者登陸賬號
char* lpPassword - 發送者登陸密碼
char* lpPhone - 接收者手機號碼
char* lpContent - 發送內容
char* lpNickName - 發送者昵稱
char* lpExtent - 擴展信息
* 輸出參數: 無
* 返 回 值: int 00 - 操作完成,結果未知
01 - 網關代號不存在
02 - 網絡連接超時
03 - 用戶中止操作
04 - 網關/賬號/手機/短信內容空白或非法
05 - 出現其他錯誤
*****************************************************************/
SENDSMS_API int CALLAGREEMENT SendSMS(char* lpGateway,
char* lpUserName,
char* lpPassword,
char* lpPhone,
char* lpContent,
char* lpNickName,
char* lpExtent
)
{
int Result;
static TCHAR hdrs[] = _T("Content-Type: application/x-www-form-urlencoded");
static TCHAR accept[] = _T("Accept: */*");
static TCHAR frmdata[1024];
// 登陸姓名,密碼等不允許為空
if ((strlen(lpGateway)<=0)||(strlen(lpUserName)<=0)||
(strlen(lpPassword)<=0)||(strlen(lpPhone)<=0)||(strlen(lpContent)<=0))
return 4;
// 選擇網易網關發送
if (strcmp(lpGateway, "163.com")==0)
{
// 登錄短信發送系統
sprintf(frmdata, "username=%s&password=%s", lpUserName, lpPassword);
Result = doHTTP(hdrs, accept, "POST", frmdata, "reg4.163.com", "/in.jsp");
// 發送短信
if (strlen(lpNickName)>0)
sprintf(frmdata, "send=1&phone=%s&message=%s--%s", lpPhone, lpContent, lpNickName);
else
sprintf(frmdata, "send=1&phone=%s&message=%s", lpPhone, lpContent);
Result = doHTTP(hdrs, accept, "POST", frmdata, "sms.163.com", "/service/sendmsg_pop.php");
// 退出短信發送系統
sprintf(frmdata, "username=%s", lpUserName);
Result = doHTTP(hdrs, accept, "GET", frmdata, "reg4.163.com", "/Logout.jsp");
return Result;
}
// 選擇搜狐網關發送
if (strcmp(lpGateway, "sohu.com")==0)
{
Result = 1;
return Result;
}
// 網關代號不存在
return 1;
}
新聞熱點
疑難解答