C++實現Http Post請求

來源:互聯網
上載者:User

參考資料:

http://apps.hi.baidu.com/share/detail/39003388

http://blog.csdn.net/yc0188/article/details/4741871

http://bbs.chinaunix.net/thread-2094334-1-1.html


//標頭檔

#include <iostream>
#include <string>
#include <Winsock2.h>

using namespace std;


//函式宣告

int request(char* hostname, char* api, char* parameters);


//方法調用

int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
    int nRetCode = 0;


    // initialize MFC and print and error on failure
    if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
    {
        // TODO: change error code to suit your needs
        _tprintf(_T("Fatal Error: MFC initialization failed\n"));
        return 1;
    }
    
    request("reg.163.com", "http://reg.163.com/CheckUser.jsp", "test");

    return nRetCode;
}


//函數實現
int request(char* hostname, char* api, char* parameters)
{
    WSADATA WsaData;
    WSAStartup(0x0101, &WsaData);


    //初始化socket
    struct hostent* host_addr = gethostbyname(hostname);
    if (host_addr == NULL)
    {
        cout<<"Unable to locate host"<<endl;
        return -103;
    }


    sockaddr_in sin;
    sin.sin_family = AF_INET;
    sin.sin_port = htons((unsigned short)80);
    sin.sin_addr.s_addr = *((int*)*host_addr->h_addr_list);


    int sock = socket(AF_INET, SOCK_STREAM, 0);
    if (sock == -1)
    {
        return -100;
    }


    //建立串連
    if (connect(sock, (const struct sockaddr *)&sin, sizeof(sockaddr_in) ) == -1)
    {
        cout<<"connect failed"<<endl;
        return -101;
    }


    //初始化發送資訊
    char send_str[2048] = {0};


    //頭資訊
    strcat(send_str, "POST ");
    strcat(send_str, api);
    strcat(send_str, " HTTP/1.1\r\n");
    strcat(send_str, "Host: ");
    strcat(send_str, hostname);
    strcat(send_str, "\r\n");
    strcat(send_str, "Connection: keep-alive\r\n");


    char content_header[100];
    sprintf(content_header,"Content-Length: %d\r\n", strlen(parameters));


    strcat(send_str, content_header);
    strcat(send_str, "Cache-Control: max-age=0\r\n");
    strcat(send_str, "Origin: http://www.hao123.com\r\n");
    strcat(send_str, "User-Agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/15.0.849.0 Safari/535.1\r\n");
    strcat(send_str, "Content-Type: application/x-www-form-urlencoded\r\n");
    strcat(send_str, "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n");
    strcat(send_str, "Referer: http://www.hao123.com/\r\n");
    strcat(send_str, "Accept-Encoding: gzip,deflate,sdch\r\n");
    strcat(send_str, "Accept-Language: zh-CN,zh;q=0.8\r\n");


    //內容資訊
    strcat(send_str, "\r\n");
    strcat(send_str, parameters);


    if (send(sock, send_str, strlen(send_str),0) == -1)
    {
        cout<<"send failed"<<endl;
        return -101;
    }


    //擷取返回資訊
    char recv_str[4096] = {0};
    if (recv(sock, recv_str, sizeof(recv_str), 0) == -1)
    {
        cout<<"recv failed"<<endl;
        return -101;
    }


    cout<<recv_str<<endl;


    WSACleanup( );


    return 0;
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.