• C++使用http请求,亲测可用,VS2008编译运行通过


    [cpp] view plain copy
     
    1. int CMyFunctionsDlg::request(char* hostname, char* api, char* parameters)  
    2. {  
    3.     WSADATA WsaData;  
    4.     WSAStartup(0x0101, &WsaData);  
    5.   
    6.   
    7.     //初始化socket  
    8.     struct hostent* host_addr = gethostbyname(hostname);  
    9.     if (host_addr == NULL)  
    10.     {  
    11. //      cout<<"Unable to locate host"<<endl;  
    12.         return -103;  
    13.     }  
    14.   
    15.   
    16.     sockaddr_in sin;  
    17.     sin.sin_family = AF_INET;  
    18.     sin.sin_port = htons((unsigned short)80);  
    19.     sin.sin_addr.s_addr = *((int*)*host_addr->h_addr_list);  
    20.   
    21.   
    22.     int sock = socket(AF_INET, SOCK_STREAM, 0);  
    23.     if (sock == -1)  
    24.     {  
    25.         return -100;  
    26.     }  
    27.   
    28.   
    29.     //建立连接  
    30.     if (connect(sock, (const struct sockaddr *)&sin, sizeof(sockaddr_in) ) == -1)  
    31.     {  
    32. //        cout<<"connect failed"<<endl;  
    33.         return -101;  
    34.     }  
    35.   
    36.   
    37.     //初始化发送信息  
    38.     char send_str[2048] = {0};  
    39.   
    40.   
    41.     //头信息  
    42.     strcat(send_str, "POST ");  
    43.     strcat(send_str, api);  
    44.     strcat(send_str, " HTTP/1.1 ");  
    45.     strcat(send_str, "Host: ");  
    46.     strcat(send_str, hostname);  
    47.     strcat(send_str, " ");  
    48.     strcat(send_str, "Connection: keep-alive ");  
    49.   
    50.   
    51.     char content_header[100];  
    52.     sprintf(content_header,"Content-Length: %d ", strlen(parameters));  
    53.   
    54.   
    55.     strcat(send_str, content_header);  
    56.     strcat(send_str, "Cache-Control: max-age=0 ");  
    57.     strcat(send_str, "Origin: http://www.hao123.com ");  
    58.     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 ");  
    59.     strcat(send_str, "Content-Type: application/x-www-form-urlencoded ");  
    60.     strcat(send_str, "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 ");  
    61.     strcat(send_str, "Referer: http://www.hao123.com/ ");  
    62.     strcat(send_str, "Accept-Encoding: gzip,deflate,sdch ");  
    63.     strcat(send_str, "Accept-Language: zh-CN,zh;q=0.8 ");  
    64. //  strcat(send_str, "Expect: 100-continue ");  
    65.   
    66.   
    67.     //内容信息  
    68.     strcat(send_str, " ");  
    69.     strcat(send_str, parameters);  
    70.   
    71.   
    72.     if (send(sock, send_str, strlen(send_str),0) == -1)  
    73.     {  
    74. //        cout<<"send failed"<<endl;  
    75.         return -101;  
    76.     }  
    77.   
    78.   
    79.     //获取返回信息  
    80.     char recv_str[4096] = {0};  
    81.     if (recv(sock, recv_str, sizeof(recv_str), 0) == -1)  
    82.     {  
    83. //        cout<<"recv failed"<<endl;  
    84.         return -101;  
    85.     }  
    86.   
    87.   
    88. //    cout<<recv_str<<endl;  
    89.   
    90.   
    91.     WSACleanup( );  
    92.   
    93.   
    94.     return 0;  
    95. }  

    request("www.xxxx.com", "http://www.xxxx.com/client/data.php", "{"request":"userLogin","posts":[{"loginName":"123456","password":"7890"}]}");

    http://blog.csdn.net/dingxz105090/article/details/41285149

  • 相关阅读:
    记一份电网信息化建设企业信息分析平台规划
    2018年个人心灵历程记录
    OGG For Bigdata To Kafka同步问题处理
    Vue.js有赞商城(思路以及总结整理)
    汉诺塔-递归算法
    git合并分支
    js实现页面消息滚动效果
    chrome实现网页高清截屏(F12、shift+ctrl+p、capture)
    JS计算时间差
    Socket.io详解
  • 原文地址:https://www.cnblogs.com/findumars/p/5928686.html
Copyright © 2020-2023  润新知