当前位置: 首页 > 工具软件 > cpp-netlib > 使用案例 >

[cpp-netlib]如何发送post请求

陆弘新
2023-12-01
int CHttpFetch::Post(const CString& strURL, const CString& strContent, CString& strResponse)
{
    int nStatus = 200;

    try
    {
        http::client::request request(StringUtility::CStr2Stl(strURL));

        std::string content = StringUtility::CStr2Stl(strContent);
        CString strContentLength;
        strContentLength.Format(_T("%d"), content.length());
        request << header("Content-Length", StringUtility::CStr2Stl(strContentLength));
        request << header("Content-Type", "application/x-www-form-urlencoded");
        request << body(content);

        http::client::response response = m_client.post(request);
        strResponse = StringUtility::Stl2CStr(chunked_body(response));

        nStatus = status(response);
    }
    catch (std::exception& e)
    {
        UNREFERENCED_PARAMETER(e);
        nStatus = 500; 
    }

    return nStatus;
}

 类似资料: