程序问答   发布时间:2022-06-01  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了WinHttpSendRequest 持续失败并显示 Windows 错误代码 87、ERROR_INVALID_PARAMETER (C++)大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决WinhttpSendrequest 持续失败并显示 Windows 错误代码 87、ERROR_INVALID_PARAMETER (C++)?

开发过程中遇到WinhttpSendrequest 持续失败并显示 Windows 错误代码 87、ERROR_INVALID_PARAMETER (C++)的问题如何解决?下面主要结合日常开发的经验,给出你关于WinhttpSendrequest 持续失败并显示 Windows 错误代码 87、ERROR_INVALID_PARAMETER (C++)的解决方法建议,希望对你解决WinhttpSendrequest 持续失败并显示 Windows 错误代码 87、ERROR_INVALID_PARAMETER (C++)有所启发或帮助;

我正在尝试使用 Winhttp 通过具有标头和可选的 Json 内容的代理发送 http post 请求。我已经尝试了此处问题中概述的解决方案:

WinhttpSendrequest returns ERROR_INVALID_PARAMETER

https://picsum.photos/

然而,它似乎并没有改变任何东西。我在下面粘贴了发布请求功能。也许我错过了一些东西。如果我能做些什么来解决这个问题,请告诉我。

问题区域围绕 WinhttpSendrequest

std::string Postrequest(std::wString token,std::string Json,std::string proxy,std::wString url,std::wString domain) {
    std::cout<<"Postrequest function started"<<std::endl;

    std::cout<<"Json is being processed"<<std::endl;
    LPSTR  data = (LPSTR)Json.c_str();
    DWORD data_len;
    if(data!=0) data_len = strlen(data); else data_len = 0;

    std::cout<<"Json done"<<std::endl<<GetLastError()<<std::endl;

    std::string response;

    DWORD DWSize = 0;
    DWORD DWDownloaded = 0;
    LPSTR pszOutBuffer;
    BOol  bResults = falSE;
    HINTERNET  hConnect = NulL,hrequest = NulL;

    std::wString stemp = std::wString(proxy.begin(),proxy.end());
    const wchar_t* sw = stemp.c_str();

    std::cout<<"initializing WinhttpOpen"<<std::endl<<GetLastError()<<std::endl;

    HINTERNET hSession=WinhttpOpen(L"Winhttp Example/1.0",WINhttp_ACCESS_TYPE_DEFAulT_PROXY,L"http://"+*sw,WINhttp_NO_PROXY_BYPASS,0);

    std::cout<<"initializing WinhttpConnect"<<std::endl<<GetLastError()<<std::endl;

    if(hSession) hConnect = WinhttpConnect(hSession,domain.c_str(),INTERNET_DEFAulT_httpS_PORT,0 );

    std::cout<<"initializing WinhttpOpenrequest"<<std::endl<<GetLastError()<<std::endl;

    if(hConnect) hrequest = WinhttpOpenrequest(hConnect,L"POST",url.c_str(),NulL,WINhttp_NO_REFERER,WINhttp_DEFAulT_ACCEPT_TYPES,WINhttp_FLAG_SECURE);

    const wchar_t *additionalheaders =
        L"Content-Type: application/Json\r\n"
        L"User-Agent: Mozilla/5.0 (X11; linux x86_64) AppleWebKit/537.11 (KHTML,like Gecko) Chrome/23.0.1271.64 Safari/537.11\r\n"
        L"Authorization: "+*token.c_str()+*L"\r\n";
    DWORD headersLength = -1;

    std::cout<<"initializing WinhttpSendrequest"<<std::endl<<GetLastError()<<std::endl;
    std::cout<<"bResults: "<<bResults<<std::endl;

    LPVOID rqdata;
    if(data==0)rqdata = WINhttp_NO_requEST_DATA;
    else rqdata=(LPVOID)data;

    if (hrequest) bResults = WinhttpSendrequest(hrequest,additionalheaders,headersLength,rqdata,data_len,0);
    else std::cout<<"else 1"<<std::endl;

    std::cout<<"bResults: "<<bResults<<std::endl;
    std::cout<<"initializing WinhttpReceiveResponse"<<std::endl<<GetLastError()<<std::endl;

    if (bResults) bResults = WinhttpReceiveResponse(hrequest,null);
    else std::cout<<"else 2"<<std::endl;

    std::cout<<"bResults: "<<bResults<<std::endl;

    if (bResults)
        {
            do
            {
                // check for available data.
                DWSize = 0;
                if (!WinhttpqueryDataAvailable(hrequest,&dWSizE))
                    std::cout<<"Error in WinhttpqueryDataAvailable.\n"<<GetLastError();

                // Allocate space for the buffer.
                pszOutBuffer = new char[DWSize + 1];
                if (!pszOutBuffer)
                {
                    printf("Out of memory\n");
                    DWSize = 0;
                }
                else
                {
                    // Read the data.
                    ZeroMemory(pszOutBuffer,DWSize + 1);

                    if (!WinhttpReadData(hrequest,(LPVOID)pszOutBuffer,DWSize,&dWDownloaded))
                        std::cout<<"Error in WinhttpReadData.\n"<<GetLastError();
                    else
                        response = response + std::string(pszOutBuffer);
                    // Free the memory allocated to the buffer.
                    delete[] pszOutBuffer;
                }
            } while (DWSize > 0);
        }
        if (!bResults)
            std::cout<<"Error has occurred.\n"<<GetLastError()<<std::endl;

        if (hrequest) WinhttpCloseHandle(hrequest);
        if (hConnect) WinhttpCloseHandle(hConnect);
        if (hSession) WinhttpCloseHandle(hSession);

        return response;
}

所有的 cout 都是为了调试,稍后我会删除。

输出看起来像这样:(对不起,如果有点乱)

Postrequest function started
Json is being processed
Json done
0
initializing WinhttpOpen
0
initializing WinhttpConnect
0
initializing WinhttpOpenrequest
0
initializing WinhttpSendrequest
0
bResults: 0
bResults: 0
initializing WinhttpReceiveResponse
87
else 2
bResults: 0
Error has occurred.
87

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

大佬总结

以上是大佬教程为你收集整理的WinHttpSendRequest 持续失败并显示 Windows 错误代码 87、ERROR_INVALID_PARAMETER (C++)全部内容,希望文章能够帮你解决WinHttpSendRequest 持续失败并显示 Windows 错误代码 87、ERROR_INVALID_PARAMETER (C++)所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。