【问题标题】:New HttpClient proxy settings issues新的 HttpClient 代理设置问题
【发布时间】:2012-06-27 14:18:39
【问题描述】:

我正在尝试根据新的 fw 4.5 功能(例如 HttpClient 和 await/async)重写旧的网络身份验证逻辑,并且我在请求和响应之间遇到了意外的延迟(大约 15 秒)。我猜这是因为客户端试图从 IE 中查找/使用代理,就像使用旧的 HttpRequest/WebClient 一样。这是代码:

public static async Task<AuthResult> GetDataFromServiceAsync(string url, string login, string password)
{
     Debug.WriteLine("[" + DateTime.Now + "] GetDataFromServiceAsync");
     var handler = new HttpClientHandler { Credentials = new NetworkCredential(login, password)/*, UseProxy = false*/ };
     var client = new HttpClient(handler) { MaxResponseContentBufferSize = Int32.MaxValue };
     try
     {
         var resp = await client.GetAsync(new Uri(url));
         var content = resp.Content.ReadAsStringAsync().Result;
         var auth = ParseContent(content);
         Debug.WriteLine("[" + DateTime.Now + "] Returning AuthResult : " + auth);
         return new AuthResult { Success = auth, Exception = null};
     }
     catch (Exception exception)
     {
         //
         Debug.WriteLine("[" + DateTime.Now + "] Returning error AuthResult : " + exception.Message);
         return new AuthResult { Success = false, Exception = exception }; ;
         }
     }
}

此方法与 api 中的其他方法包装在一起,实际上与当前情况无关:

public async Task<AuthResult> IsAuthenticated(string login, string password)
{
    Debug.WriteLine("[" + DateTime.Now + "] Starting ISAuthenticationService.IsAuthenticated");
    // ... (cut) ...
    // async
    var authResult = await ISHelpers.GetDataFromServiceAsync(url, login, password);
    Debug.WriteLine("[" + DateTime.Now + "] Ending ISAuthenticationService.IsAuthenticated");
    return authResult;
}

身份验证发生在各自的 ViewModel 命令中:

private async void ExecuteAuthenticationCommand()
{
    // Test stub
    //var authService = new Helpers.MockupAuthenticationService();
    var authService = new Helpers.ISAuthenticationService();
    var auth = await authService.IsAuthenticated(Login, Password);
    if (auth.Success)
    {
        MessageBox.Show("Authentication success");
        Messenger.Default.Send<LoginDataItem>(_dataItem);
    }
    else
    {
        MessageBox.Show(auth.Exception.Message, "Incorrect login data");
    }
 }

调试输出:

[27.06.2012 16:54:10] Starting ISAuthenticationService.IsAuthenticated
[27.06.2012 16:54:10] GetDataFromServiceAsync
[27.06.2012 16:54:25] ParseContent in GetDataFromServiceAsync
[27.06.2012 16:54:25] Returning AuthResult : True
[27.06.2012 16:54:25] Ending ISAuthenticationService.IsAuthenticated

当我在 HttpClientHandler 设置中取消注释 UseProxy = false 时,延迟消失并且身份验证没有延迟。即使我没有取消注释 UseProxy (每十次运行一次),有时也会出现同样的问题。问题是 - 这是一个错误还是什么?尝试从服务器端调试,发现轮询请求之间没有差异。提前致谢。

【问题讨论】:

    标签: .net networking .net-4.5 async-await dotnet-httpclient


    【解决方案1】:

    这不是错误。 IE 的默认设置是尝试自动检测代理,这可能需要 30 秒。要禁用自动检测,您需要将 UseProxy 设置为 False。

    其实这些设置其实和IE没什么关系。只是 IE 使用(和设置)系统的默认设置。 HttpClient 和 WebClient 都使用系统的默认设置,除非您覆盖它们。

    至于检测速度,取决于系统设置。如果您在 IE 或 Chrome 中禁用自动代理检测,您会注意到您的浏览器在重新启动后第一次打开的速度要快得多。这是因为它不会尝试检测代理。代理检测过程在Automatic Proxy Detection中有描述,涉及几个步骤:

    1. 找到最后使用的代理配置脚本
    2. 从 DHCP 发现代理
    3. 使用 DNS 查找名为 WAPD 的计算机

    第 2 步和第 3 步可能需要很长时间,具体取决于您的网络基础设施,甚至可能涉及超时。

    【讨论】:

    • 为什么有时检测会瞬间发生?为什么HttpClient甚至与IE有关?阅读 MSDN 并没有找到任何参考资料。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-06-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-08
    • 2017-07-13
    • 1970-01-01
    相关资源
    最近更新 更多