2

我有几个用户在尝试激活我的一个程序时遇到错误,并且返回的错误代码的描述是“无法解析服务器名称”。我让 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 );
    }
}
4

0 回答 0