天气与日历 切换到窄版

 找回密码
 立即注册
中国膜结构网
十大进口膜材评选 十大国产膜材评选 十大膜结构设计评选 十大膜结构公司评选
查看: 10|回复: 0

c++ 发送http请求

[复制链接]
  • TA的每日心情
    开心
    昨天 15:23
  • 签到天数: 69 天

    [LV.6]常住居民II

    410

    主题

    167

    回帖

    2704

    积分

    管理员

    积分
    2704
    发表于 2024-6-22 09:46:18 | 显示全部楼层 |阅读模式
    c++ 发送http请求



    #include <afxwin.h>
    #include <stdio.h>
    #include <windows.h>
    #include <string>
    #include "Wininet.h"
    #pragma comment(lib,"Wininet.lib")

    //模拟浏览器发送HTTP请求函数
    std::string HttpRequest(char * lpHostName,short sPort,char * lpUrl,char * lpMethod,char * lpPostData,int nPostDataLen)
    {
        HINTERNET hInternet,hConnect,hRequest;

        BOOL bRet;

        std::string strResponse;

        hInternet = (HINSTANCE)InternetOpen("User-Agent",INTERNET_OPEN_TYPE_PRECONFIG,NULL,NULL,0);
        if(!hInternet)
            goto Ret0;

        hConnect = (HINSTANCE)InternetConnect(hInternet,lpHostName,sPort,NULL,"HTTP/1.1",INTERNET_SERVICE_HTTP,0,0);
        if(!hConnect)
            goto Ret0;

        hRequest = (HINSTANCE)HttpOpenRequest(hConnect,lpMethod,lpUrl,"HTTP/1.1",NULL,NULL,INTERNET_FLAG_RELOAD,0);
        if(!hRequest)
            goto Ret0;

        //bRet = HttpAddRequestHeaders(hRequest,"Content-Type: application/x-www-form-urlencoded",Len(FORMHEADERS),HTTP_ADDREQ_FLAG_REPLACE | HTTP_ADDREQ_FLAG_ADD);
        //if(!bRet)
            //goto Ret0;

        bRet = HttpSendRequest(hRequest,NULL,0,lpPostData,nPostDataLen);
        while(TRUE)
        {
            char cReadBuffer[4096];
            unsigned long lNumberOfBytesRead;
            bRet = InternetReadFile(hRequest,cReadBuffer,sizeof(cReadBuffer) - 1,&lNumberOfBytesRead);
            if(!bRet || !lNumberOfBytesRead)
                break;
            cReadBuffer[lNumberOfBytesRead] = 0;
            strResponse = strResponse + cReadBuffer;
        }

    Ret0:
        if(hRequest)
            InternetCloseHandle(hRequest);
        if(hConnect)
            InternetCloseHandle(hConnect);
        if(hInternet)
            InternetCloseHandle(hInternet);

        return strResponse;
    }

    void main()
    {
        //CString strResponse = HttpRequest("translate.google.com",80,"/translate_t?langpair=en|zh-CN","POST","hl=zh-CN&ie=UTF-8&text=this is me&langpair=en|zh-CN",strlen("hl=zh-CN&ie=UTF-8&text=this is me&langpair=en|zh-CN"));
        std::string strResponse = HttpRequest("www.hao123.com",80,NULL,"GET",NULL,0);
        FILE * fp = fopen("C:\123.htm","wb");
        fwrite(strResponse.c_str(),strResponse.length(),1,fp);
        fclose(fp);
    }

     

     

     

     

    c++ 发送http请求
    中国膜结构网打造全中国最好的膜结构综合平台 ,统一协调膜结构设计,膜结构施工,膜材采购,膜材定制,膜结构预算全方位服务。 中国空间膜结构协会合作单位。
    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    QQ|Archiver|手机版|中国膜结构网|中国膜结构协会|进口膜材|国产膜材|ETFE|PVDF|PTFE|设计|施工|安装|车棚|看台|污水池|

    GMT+8, 2024-7-1 05:33 , Processed in 0.056592 second(s), 22 queries .

    Powered by Discuz! X3.5

    © 2001-2024 Discuz! Team.

    快速回复 返回顶部 返回列表