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;
}

 

posted @ 2018-03-06 11:34  sz_leez  阅读(1317)  评论(0编辑  收藏  举报