国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

使用WinInet編寫發送短信程序

2019-11-18 19:57:41
字體:
來源:轉載
供稿:網友

  如果能夠使自己程序具有短信發送功能,那將會使您的程序蓬蓽生輝,特別是通訊錄等程序。通常在程序中發送短信有兩種方式,一種是使用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;
}


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 松江区| 利辛县| 新余市| 红桥区| 西乌珠穆沁旗| 桑植县| 仁寿县| 汉川市| 呈贡县| 绿春县| 葫芦岛市| 武平县| 惠州市| 西充县| 长寿区| 大渡口区| 公安县| 滦平县| 黎平县| 庆安县| 兴隆县| 乌兰察布市| 大宁县| 枞阳县| 湘阴县| 海口市| 台北县| 定西市| 揭阳市| 松滋市| 南陵县| 威远县| 榆社县| 启东市| 博湖县| 横峰县| 台东市| 开阳县| 灵武市| 皋兰县| 伊宁县|