• HTTP POST请求


    接口实现

    CString HttpPostRequest(CString strServerName, int nPort, CString strUrl, CString sParam)
    {
        if (strServerName.IsEmpty())
        {
            return "";
        }
    
        if (strServerName.Mid(strServerName.GetLength() - 1, 1) == "/")
        {
            strServerName = strServerName.Mid(0, strServerName.GetLength() - 1);
        }
        if (strServerName.Find(L"http://") == 0)
        {
            int nLength = strlen("http://");
            strServerName = strServerName.Mid(nLength, strServerName.GetLength() - nLength);
        }
        if (strServerName.Find(L"https://") == 0)
        {
            int nLength = strlen("https://");
            strServerName = strServerName.Mid(nLength, strServerName.GetLength() - nLength);
        }
    
        HINTERNET hInternet = (HINSTANCE)InternetOpen(NULL, INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
        if (!hInternet)
        {
            return "";
        }
    
        HINTERNET hConnection = (HINSTANCE)InternetConnect(hInternet, strServerName, nPort, NULL, L"HTTP/1.1", INTERNET_SERVICE_HTTP, 0, 0);
        if (!hConnection)
        {
            if (hInternet) InternetCloseHandle(hInternet);
            return "";
        }
    
        HINTERNET hRequest = (HINSTANCE)HttpOpenRequest(hConnection, L"POST", strUrl, L"HTTP/1.1", NULL, NULL, INTERNET_FLAG_KEEP_CONNECTION, 0);
        if (!hRequest)
        {
            if (hConnection) InternetCloseHandle(hConnection);
            if (hInternet) InternetCloseHandle(hInternet);
            return "";
        }
    
        //HTTP 请求报头
        HttpAddRequestHeaders(hRequest, L"Content-Type: application/x-www-form-urlencoded", -1, HTTP_ADDREQ_FLAG_ADD);
        HttpAddRequestHeaders(hRequest, L"Accept-Language: zh-cn", -1, HTTP_ADDREQ_FLAG_ADD);
        //支持gzip请求数据
        HttpAddRequestHeaders(hRequest, L"Accept-Encoding: gzip, deflate", -1, HTTP_ADDREQ_FLAG_ADD);
    
        string sBuf = CSTOS(sParam);
        bool bRet = HttpSendRequest(hRequest, NULL, -1, (LPVOID)sBuf.c_str(), sBuf.length());
    
        //读取服务端返回数据
        const int nBuffLen = 1024;
        char *buffer       = new char[nBuffLen];
        buffer[0]          = 0;
    
        string strResponse;
        strResponse.reserve(nBuffLen);
    
        DWORD dwTempReadLen      = 0;
        DWORD dwTotalResponseLen = 0;
        while (::InternetReadFile(hRequest, buffer, nBuffLen - 1, &dwTempReadLen) && dwTempReadLen > 0)
        {
            dwTotalResponseLen += dwTempReadLen;
            strResponse.append(buffer, dwTempReadLen);
            buffer[0] = 0;
        }
        delete[]buffer;
    
        if (hRequest) InternetCloseHandle(hRequest);
        if (hConnection) InternetCloseHandle(hConnection);
        if (hInternet) InternetCloseHandle(hInternet);
    
        CString csResponseResult = UTOCS(strResponse);
        bool bgzip = false;
        //判断返回数据是否为gzip[gzip文件开头是1f8b]
        if (dwTotalResponseLen >= 2
            && static_cast<unsigned char>(strResponse[0]) == 0x1f
            && static_cast<unsigned char>(strResponse[1]) == 0x8b)
        {
            bgzip = true;
        }
        
        if (bgzip)
        {
            //gzip数据解压
            std::string decompressedString;
            filtering_ostream fos;
            fos.push(gzip_decompressor());
            fos.push(boost::iostreams::back_inserter(decompressedString));
            fos << strResponse;
            fos << std::flush; 
            csResponseResult = UTOCS(decompressedString);
        }    
    
        return csResponseResult;
    }
  • 相关阅读:
    9个数中取最大值最小值速度问题
    ubuntu 12.04安装git 1.8.11
    <转>Win7资源管理器更新后不断重启解决方案
    windows下安装安卓开发环境和NDK支持
    饱和算法
    bzip21.0.6
    《转》GetFileTitle与文件扩展名是否显示有关
    Ubuntu设置环境变量PATH的三种方法 <转>
    ubuntu下使用脚本交叉编译windows下使用的ffmpeg
    UnxUtils windows下linux命令
  • 原文地址:https://www.cnblogs.com/sz-leez/p/8513468.html
Copyright © 2020-2023  润新知