程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> 調用飛信HTTP接口給自己發短信,調用接口發短信

調用飛信HTTP接口給自己發短信,調用接口發短信

編輯:C++入門知識

調用飛信HTTP接口給自己發短信,調用接口發短信


注:

1、下文中所有HTTP請求所指的Host都是f.10086.cn

2、目前只有中國移動用戶可以使用

1、打開登錄頁面:GET /huc/user/space/login.do?m=submit&fr=space,獲取兩個cookie值:JSESSIONID和UUID
2、登錄:POST /huc/user/space/login.do,數據為手機號碼和密碼:mobilenum=your_phone_number&password=your_fetioon_password&m=submit&backurl=http%3A%2F%2Ff.10086.cn%2F&fr=space,獲取cookie值:Set-Cookie: cell_cookie,注意:登錄時,需要攜帶header:Content-Type: application/x-www-form-urlencoded,否則會觸發驗證碼檢查
2.1、上述步驟2會重定向到其他路徑:Location: http://f.10086.cn/?nuc_id=5cb9e9eca65e2bc2875a6fe55869daa1,4e8cbe602e50b189ddc33e51de704474,e017a7e72b081563f1fbf1263d2fd8f8,1
2.2、http://f.10086.cn/?nuc_id=5cb9e9eca65e2bc2875a6fe55869daa1,4e8cbe602e50b189ddc33e51de704474,e017a7e72b081563f1fbf1263d2fd8f8,1<----這個會繼續重定向到Location: http://f.10086.cn/wap2.jsp,同時會重置cookie值:JSESSIONID。wap2.jsp是一個新聞綜合網頁。
3、發送消息:POST /im/user/sendMsgToMyselfs.action HTTP/1.1,數據:msg=A hello word short message
3.1、上述步驟可能會被重定向到Location: http://f.10086.cn/im/login/cklogin.action?t=1420875966727,此時可以重新POST /im/user/sendMsgToMyselfs.action HTTP/1.1,即可發送成功。

 

一下是使用MFC Wininet實現的Demo,在本文章發布時,還是可以發送短信的

另外,代碼沒有經過錘煉,請不要在意內存洩露等bug。。。

#define FE_BUFFER_SIZE (1024*1024)

void do_me()
{
    HINTERNET hSessHnd;
    HINTERNET hConnHnd;
    HINTERNET hRqstHnd;
    BOOL      bRqstRet;
    BOOL      bQueryRet;
    BOOL      bReadRet;
    UCHAR*    pucBuffer = new UCHAR[FE_BUFFER_SIZE];
    DWORD     dwBufferLen;
    DWORD     dwReadLen;
    DWORD     dwIndex;

    //使用fiddler代理
    hSessHnd = InternetOpenA("FetionMsg", INTERNET_OPEN_TYPE_PROXY, "http=http://127.0.0.1:8888", NULL, 0);
    //直接登錄
    //hSessHnd = InternetOpenA("FetionMsg", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
    if (NULL == hSessHnd)
    {
        printf("InternetOpenA failed\r\n");
        return;
    }

    hConnHnd = InternetConnectA(hSessHnd, "f.10086.cn", INTERNET_DEFAULT_HTTP_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
    if (NULL == hConnHnd)
    {
        printf("InternetConnectA failed.\r\n");
        return;
    }

    hRqstHnd = HttpOpenRequestA(hConnHnd, "GET", "huc/user/space/login.do?m=submit&fr=space", "HTTP/1.1", NULL, NULL, INTERNET_FLAG_RELOAD, 0);
    if (NULL == hRqstHnd)
    {
        printf("HttpOpenRequestA failed.\r\n");
        return;
    }

    bRqstRet = HttpSendRequestA(hRqstHnd, NULL, 0, NULL, 0);
    if (TRUE != bRqstRet)
    {
        printf("HttpSendRequestA failed\r\n");
        return;
    }

    ZeroMemory(pucBuffer, FE_BUFFER_SIZE);
    dwBufferLen = FE_BUFFER_SIZE;
    dwIndex = 0;
    bQueryRet = HttpQueryInfoA(hRqstHnd, HTTP_QUERY_CONTENT_LENGTH, pucBuffer, &dwBufferLen, &dwIndex);
    if (TRUE == bQueryRet)
    {
        printf("1: len = %s, %u\r\n", pucBuffer, dwBufferLen);
        bReadRet = InternetReadFile(hRqstHnd, pucBuffer, FE_BUFFER_SIZE, &dwReadLen);
        printf("1 read ret=%u, len=%u\r\n", bReadRet, dwReadLen);
        printf("--------\r\n");
        printf("%s", pucBuffer);
        printf("--------\r\n");
    }
    else
    {
        printf("1 no response body\r\n");
    }
    
    InternetCloseHandle(hRqstHnd);

    hRqstHnd = HttpOpenRequestA(hConnHnd, "POST", "huc/user/space/login.do", "HTTP/1.1", NULL, NULL, INTERNET_FLAG_RELOAD, 0);
    if (NULL == hRqstHnd)
    {
        printf("HttpOpenRequestA 2 failed.\r\n");
        return;
    }
    HttpAddRequestHeadersA(hRqstHnd, "Content-Type: application/x-www-form-urlencoded\r\n", -1, HTTP_ADDREQ_FLAG_ADD);

    const char* pcLoginData = "mobilenum=your_number&password=your_fetion_password&m=submit&backurl=http%3A%2F%2Ff.10086.cn%2F&fr=space";
    bRqstRet = HttpSendRequestA(hRqstHnd, NULL, 0, (VOID*)pcLoginData, strlen(pcLoginData));
    if (TRUE != bRqstRet)
    {
        printf("HttpSendRequestA 2 failed\r\n");
        return;
    }

    ZeroMemory(pucBuffer, FE_BUFFER_SIZE);
    dwBufferLen = FE_BUFFER_SIZE;
    dwIndex = 0;
    bQueryRet = HttpQueryInfoA(hRqstHnd, HTTP_QUERY_CONTENT_LENGTH, pucBuffer, &dwBufferLen, &dwIndex);
    if (TRUE == bQueryRet)
    {
        printf("2: len = %s, %u\r\n", pucBuffer, dwBufferLen);
        bReadRet = InternetReadFile(hRqstHnd, pucBuffer, FE_BUFFER_SIZE, &dwReadLen);
        printf("2 read ret=%u, len=%u\r\n", bReadRet, dwReadLen);
        printf("--------\r\n");
        printf("%s", pucBuffer);
        printf("--------\r\n");
    }
    else
    {
        printf("2 no response body\r\n");
    }
    InternetCloseHandle(hRqstHnd);

    hRqstHnd = HttpOpenRequestA(hConnHnd, "POST", "im/user/sendMsgToMyselfs.action", "HTTP/1.1", NULL, NULL, 0, 0);
    if (NULL == hRqstHnd)
    {
        printf("HttpOpenRequestA 3 failed.\r\n");
        return;
    }

    const char* pcMsgData = "msg=HelloWorld";
    bRqstRet = HttpSendRequestA(hRqstHnd, NULL, 0, (VOID*)pcMsgData, strlen(pcMsgData));
    if (TRUE != bRqstRet)
    {
        printf("HttpSendRequestA 3 failed\r\n");
        return;
    }
    ZeroMemory(pucBuffer, FE_BUFFER_SIZE);
    dwBufferLen = FE_BUFFER_SIZE;
    dwIndex = 0;
    bQueryRet = HttpQueryInfoA(hRqstHnd, HTTP_QUERY_CONTENT_LENGTH, pucBuffer, &dwBufferLen, &dwIndex);
    if (TRUE == bQueryRet)
    {
        printf("3: len = %s, %u\r\n", pucBuffer, dwBufferLen);
        bReadRet = InternetReadFile(hRqstHnd, pucBuffer, FE_BUFFER_SIZE, &dwReadLen);
        printf("3 read ret=%u, len=%u\r\n", bReadRet, dwReadLen);
        printf("--------\r\n");
        printf("%s", pucBuffer);
        printf("--------\r\n");
    }
    else
    {
        printf("3 no response body\r\n");
    }

    InternetCloseHandle(hRqstHnd);

    hRqstHnd = HttpOpenRequestA(hConnHnd, "POST", "im/user/sendMsgToMyselfs.action", "HTTP/1.1", NULL, NULL, 0, 0);
    if (NULL == hRqstHnd)
    {
        printf("HttpOpenRequestA 3 failed.\r\n");
        return;
    }
    HttpAddRequestHeadersA(hRqstHnd, "Content-Type: application/x-www-form-urlencoded\r\n", -1, HTTP_ADDREQ_FLAG_ADD);
    bRqstRet = HttpSendRequestA(hRqstHnd, NULL, 0, (VOID*)pcMsgData, strlen(pcMsgData));
    if (TRUE != bRqstRet)
    {
        printf("HttpSendRequestA 3 failed\r\n");
        return;
    }
    ZeroMemory(pucBuffer, FE_BUFFER_SIZE);
    dwBufferLen = FE_BUFFER_SIZE;
    dwIndex = 0;
    bQueryRet = HttpQueryInfoA(hRqstHnd, HTTP_QUERY_CONTENT_LENGTH, pucBuffer, &dwBufferLen, &dwIndex);
    if (TRUE == bQueryRet)
    {
        printf("3: len = %s, %u\r\n", pucBuffer, dwBufferLen);
        bReadRet = InternetReadFile(hRqstHnd, pucBuffer, FE_BUFFER_SIZE, &dwReadLen);
        printf("3 read ret=%u, len=%u\r\n", bReadRet, dwReadLen);
        printf("--------\r\n");
        printf("%s", pucBuffer);
        printf("--------\r\n");
    }
    else
    {
        printf("3 no response body\r\n");
    }

    printf("All OK!\r\n");

    InternetCloseHandle(hRqstHnd);
    InternetCloseHandle(hConnHnd);
    InternetCloseHandle(hSessHnd);
}

  

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved