WinHTTP发送请求返回错误12007(服务器名称无法解析)

时间:2012-08-08 14:48:26

标签: c++ winhttp

我有几个用户在尝试激活我的某个程序时遇到错误,并且返回的错误代码的说明是“服务器名称无法解析。”#39 ;我已经让WinHTTP尝试自动检测代理设置,但没有检测到任何设置。它能够建立会话,但WinHttpSendRequest失败。我等待用户提供有关其连接的更多信息,但我想我会在这里发帖,看看是否有人发现我的代码有任何问题。我在WinHTTP代码上找到有用的文档或示例非常困难,所以如果有人能推荐比MSDN更好的来源,我会永远感激。

这是我的代码初始化请求:

WINHTTP_CURRENT_USER_IE_PROXY_CONFIG ProxyConfig;

memset(&ProxyConfig, 0, sizeof(WINHTTP_CURRENT_USER_IE_PROXY_CONFIG));

BOOL bGetProxyConfig = WinHttpGetIEProxyConfigForCurrentUser(&ProxyConfig);
if (bGetProxyConfig && ProxyConfig.lpszProxy && ProxyConfig.lpszProxyBypass)
{
    m_hHttpSession = ::WinHttpOpen(  m_strAgentName.c_str(), 
                    WINHTTP_ACCESS_TYPE_NAMED_PROXY,
                    ProxyConfig.lpszProxy, 
                    ProxyConfig.lpszProxyBypass, 0);


}
else
{
    m_hHttpSession = ::WinHttpOpen(  m_strAgentName.c_str(), 
        WINHTTP_ACCESS_TYPE_DEFAULT_PROXY,
        WINHTTP_NO_PROXY_NAME, 
        WINHTTP_NO_PROXY_BYPASS, 0);

}


if (m_hHttpSession)
{
    INTERNET_PORT nPort = INTERNET_DEFAULT_HTTPS_PORT;

    if (!m_bUseSSL)
    {
        nPort = INTERNET_DEFAULT_HTTP_PORT;
    }

    if (m_hHttpSession)
        m_hInternetConnection = ::WinHttpConnect( m_hHttpSession, m_szHostName,
                                   nPort, 0);

    if (m_hInternetConnection)
    {
        WINHTTP_AUTOPROXY_OPTIONS  AutoProxyOptions;
        WINHTTP_PROXY_INFO         ProxyInfo;
        DWORD                      cbProxyInfoSize = sizeof(ProxyInfo);

        ZeroMemory( &AutoProxyOptions, sizeof(AutoProxyOptions) );
        ZeroMemory( &ProxyInfo, sizeof(ProxyInfo) );

        // Use auto-detection because the Proxy 
        // Auto-Config URL is not known.
        AutoProxyOptions.dwFlags = WINHTTP_AUTOPROXY_AUTO_DETECT;

        // Use DHCP and DNS-based auto-detection.
        AutoProxyOptions.dwAutoDetectFlags = 
            WINHTTP_AUTO_DETECT_TYPE_DHCP |
            WINHTTP_AUTO_DETECT_TYPE_DNS_A;

        // If obtaining the PAC script requires NTLM/Negotiate
        // authentication, then automatically supply the client
        // domain credentials.
        AutoProxyOptions.fAutoLogonIfChallenged = TRUE;

        //
        // Call WinHttpGetProxyForUrl with our target URL. If 
        // auto-proxy succeeds, then set the proxy info on the 
        // request handle. If auto-proxy fails, ignore the error 
        // and attempt to send the HTTP request directly to the 
        // target server (using the default WINHTTP_ACCESS_TYPE_NO_PROXY 
        // configuration, which the requesthandle will inherit 
        // from the session).
        //

        BOOL bReturn = FALSE;

        if( WinHttpGetProxyForUrl( m_hHttpSession,
            m_strFullUrl.c_str(),
            &AutoProxyOptions,
            &ProxyInfo))
        {
            // A proxy configuration was found, set it on the
            // request handle.

            if( !WinHttpSetOption( hHttpRequest, 
                WINHTTP_OPTION_PROXY,
                &ProxyInfo,
                cbProxyInfoSize ) )
            {
                // Exit if setting the proxy info failed.
                bReturn = FALSE;
            }
            else
            {
                bReturn = TRUE;
            }
        }

        if( ProxyInfo.lpszProxy != NULL )
            GlobalFree(ProxyInfo.lpszProxy);

        if( ProxyInfo.lpszProxyBypass != NULL )
            GlobalFree( ProxyInfo.lpszProxyBypass );
    }
}

0 个答案:

没有答案