Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/129.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ InternetOpenUrl的Unicode版本返回错误12002_C++_Unicode_Wininet - Fatal编程技术网

C++ InternetOpenUrl的Unicode版本返回错误12002

C++ InternetOpenUrl的Unicode版本返回错误12002,c++,unicode,wininet,C++,Unicode,Wininet,基本上,我正在将我的程序从多字节转换为unicode,这样我就可以向其他国家的人宣传它的商业性,也基本上是一个实时流应用程序。我在转换过程中遇到了很多问题。但最近,我遇到了一个让我目瞪口呆的人 我有以下代码,包括Remy Lebeau答案中的函数: void get_user_request(const wchar_t *url) { LPVOID hi, hu; hi = InternetOpenW(SUBSCRIBER_USER_AGENT, INTERNET_OPEN_T

基本上,我正在将我的程序从多字节转换为unicode,这样我就可以向其他国家的人宣传它的商业性,也基本上是一个实时流应用程序。我在转换过程中遇到了很多问题。但最近,我遇到了一个让我目瞪口呆的人

我有以下代码,包括Remy Lebeau答案中的函数:

void get_user_request(const wchar_t *url)
{
    LPVOID hi, hu;

    hi = InternetOpenW(SUBSCRIBER_USER_AGENT, INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);

    if(hi==NULL)
    {
        WinInetError(L"InternetOpenW");
    }
    else
    {
        hu = InternetOpenUrlW(hi, url, NULL, 0, INTERNET_FLAG_RELOAD, 0);

        if(hu==NULL)
        {
           WinInetError("InternetOpenUrlW");
        }
        else
        {
            MessageBoxW ( 0, "We're in", 0, 0 );
            InternetCloseHandle(hu);
        }
        InternetCloseHandle(hi);
    }
}
编辑1开始

我这样调用函数:

get_user_request(request_url);
其中请求url为:

在此连接之前。请求url也连接到wstring,如下所示:lstrcatwRequestURL,myWString.c_Str

编辑1结束

基本上,在我转换为unicode之前;这个功能运行良好。转换后。我在hu=InternetOpenUrlW行上得到以下错误:

在返回错误12002后设置断点-查找此错误后,MSDN中的描述如下:

12002错误\u互联网\u超时 请求已超时

如果请求超时,我假设传递给函数的url可能存在某种形式的错误。之后,我在windows调试器中的该参数处插入了一个断点。似乎传递给该参数的唯一字符串是“h”。。。这很奇怪,因为我在代码中回过头来,将整个url作为常量wchar_t*传递给函数,就像它根据我的参数所做的那样

在此之后,我插入MessageBoxW以显示传递给函数的url字符串的值,就在前面。令人惊讶的是,输出的字符串与我最初传入url变量的字符串完全相同

因此,我的问题如下:

什么是****


另外,非常感谢您对该场景的所有输入。

get\u user\u请求内部的错误处理是错误的,您对请求\u url的连接也是错误的。代码应该更像这样:

void DisplayWinInetError(LPCWSTR FuncName)
{
    DWORD dwErrorCode = GetLastError();
    DWORD dwLen;

    std::wostringstream ErrorMsg;
    ErrorMsg << FuncName << L" failed!";

    if (dwErrorCode == ERROR_INTERNET_EXTENDED_ERROR)
    {
        dwLen = 0;
        if (!InternetGetLastResponseInfo(&dwErrorCode, NULL, &dwLen) &&
            (GetLastError() == ERROR_INSUFFICIENT_BUFFER))
        {
            ++dwLen;
            std::vector<wchar_t> msg(dwLen);
            if (InternetGetLastResponseInfo(&dwErrorCode, &msg[0], &dwLen))
                ErrorMsg << L'\n' << std::wstring(&msg[0], dwLen);
            else
                ErrorMsg << L"\nUnable to retrieve WinInet error message! Error: " << GetLastError();
        }
        else
            ErrorMsg << L"\nUnable to retrieve WinInet error message! Error: " << GetLastError();
    }
    else
    {
        ErrorMsg << L" WinInet Error: " << dwErrorCode;

        LPWSTR lpMsg = NULL;
        dwLen = FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_ARGUMENT_ARRAY | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, dwErrorCode, 0, (LPWSTR)&lpMsg, 0, NULL);
        if (dwLen > 0)
        {
            ErrorMsg << L'\n' << std::wstring(lpMsg, dwLen);
            LocalFree(lpMsg);
        }
        else
            ErrorMsg << L"\nUnable to retrieve System error message! Error: " << GetLastError();
    }

    MessageBoxW(NULL, ErrorMsg.str().c_str(), L"WinInet error", MB_OK);
}

1您没有向我们显示调用此函数的代码,也没有显示作为url传递的值。你的代码错了。当无法引发异常时,您将捕获。如果函数失败,它将返回NULL,您可以使用GetLastError找出原因。3您的catch抛出一个变量,该变量没有任何赋值,即未初始化。修复这些问题,找出GetLastError返回的内容,然后编辑您的帖子以包含适当的错误信息。当有疑问时,一定要阅读。如果你发布的代码有效,那就太好了。如果没有,你对失败的处理是完全错误的。与礼貌道德无关-您发布的代码有缺陷,因此您提供的有关失败的任何信息都是可疑的。修复代码使其有效,然后尝试调试它-这是解决问题的方法。如果你不想这样做,你是在浪费你的时间和我们的时间。你仍然在试图捕获一个从未抛出的异常。完全摆脱try/catch,只在函数实际失败后立即调用GetLastError。你忽略了我说的一切。调用该函数不会引发异常,因此整个try..catch都是徒劳的。调用该函数,将返回值与NULL进行比较,如果返回值为NULL,则调用GetLastError并查看得到的结果。我不知道该怎么说清楚。不能使用try..catch处理不引发异常的错误。这里有一个想法-重新开始。请看,编辑您的问题,使其包含我们可以实际使用的问题,然后从那里开始。不,正如修订历史所示,您直到我最后一次发表评论后才开始提问。你现在只是在黑暗中射击。如果不使用返回值,那么对GetLastError的调用就没有意义。当你认真地试图通过实际跟踪你得到的信息来寻求帮助时,我会撤销我的投票结果。在那之前,这是浪费时间。请看我最后一条评论中的链接,然后照它说的做。在那之前,我不干了。祝你好运。我在DisplayWinInetError中添加了更多的文本。如果它无法检索系统错误消息,它现在将说明原因。
wsprintfW ( request_url, L"?id=%s", string);
void DisplayWinInetError(LPCWSTR FuncName)
{
    DWORD dwErrorCode = GetLastError();
    DWORD dwLen;

    std::wostringstream ErrorMsg;
    ErrorMsg << FuncName << L" failed!";

    if (dwErrorCode == ERROR_INTERNET_EXTENDED_ERROR)
    {
        dwLen = 0;
        if (!InternetGetLastResponseInfo(&dwErrorCode, NULL, &dwLen) &&
            (GetLastError() == ERROR_INSUFFICIENT_BUFFER))
        {
            ++dwLen;
            std::vector<wchar_t> msg(dwLen);
            if (InternetGetLastResponseInfo(&dwErrorCode, &msg[0], &dwLen))
                ErrorMsg << L'\n' << std::wstring(&msg[0], dwLen);
            else
                ErrorMsg << L"\nUnable to retrieve WinInet error message! Error: " << GetLastError();
        }
        else
            ErrorMsg << L"\nUnable to retrieve WinInet error message! Error: " << GetLastError();
    }
    else
    {
        ErrorMsg << L" WinInet Error: " << dwErrorCode;

        LPWSTR lpMsg = NULL;
        dwLen = FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_ARGUMENT_ARRAY | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, dwErrorCode, 0, (LPWSTR)&lpMsg, 0, NULL);
        if (dwLen > 0)
        {
            ErrorMsg << L'\n' << std::wstring(lpMsg, dwLen);
            LocalFree(lpMsg);
        }
        else
            ErrorMsg << L"\nUnable to retrieve System error message! Error: " << GetLastError();
    }

    MessageBoxW(NULL, ErrorMsg.str().c_str(), L"WinInet error", MB_OK);
}
void get_user_request(LPCWSTR url)
{
    HINTERNET hInet, hUrl;

    hInet = InternetOpenW(SUBSCRIBER_USER_AGENT, INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
    if (hInet == NULL)
    {
        DisplayWinInetError(L"InternetOpenW");
    }
    else
    {
        MessageBoxW(NULL, url, "Opening url", MB_OK);

        hUrl = InternetOpenUrlW(hInet, url, NULL, 0, INTERNET_FLAG_RELOAD, 0);
        if (hUrl == NULL)
        {
            DisplayWinInetError(L"InternetOpenUrlW");
        }
        else
        {
            MessageBoxW(NULL, "We're in", L"OK", MB_OK);

            // ...

            InternetCloseHandle(hUrl);
        }

        InternetCloseHandle(hInet);
    }
}
std::wstring request_url = L"http://example-site.com/user/<user-id>/?id=" + string;
get_user_request(request_url.c_str());