新的 HttpClient 代理设置问题 [英] New HttpClient proxy settings issues

查看:26
本文介绍了新的 HttpClient 代理设置问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to rewrite old network authentication logic according to new fw 4.5 features such as HttpClient and await/async and i experience unexpected latency (around 15s) between request and response. My guess is that happens because client tries to find/use proxy from IE like it was with old HttpRequest/WebClient. Here's the code :

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 的其他方法一起包装,实际上与当前情况无关:

This method is wrapped with other method from api that actually does nothing relevant to current case :

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 命令中:

Authentication happens in respective ViewModel command :

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 的注释(每运行大约一次运行一次),有时也会发生相同的问题.问题是 - 这是一个错误还是什么?尝试从服务器端调试,发现轮询请求之间没有差异.提前致谢.

When i uncomment UseProxy = false in HttpClientHandler settings, latency passes away and auth has no delay. Problem is same happens sometimes even when i don't uncomment UseProxy (one run per about dozen runs). The question is - is that a bug or what? Tried to debug that from server-side, no differences between polling requests found. Thanks in advance.

推荐答案

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

This is not a bug. IE's default setting is to try and auto-detect the proxy, which can take up to 30 seconds. To disable automatic detection you need to set UseProxy to False.

其实这些设置和IE并没有真正的关系.只是 IE 使用(并设置)了系统的默认设置.除非您覆盖它们,否则 HttpClient 和 WebClient 都使用系统的默认设置.

In fact, the settings are not really related to IE. It's just that IE uses (and sets) the system's default settings. HttpClient and WebClient both use the system's default settings unless you override them.

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

As for the detection speed, it depends on the system settings. If you disable automatic proxy detection in IE or Chrome you will notice that your browser opens much faster the first time after a reboot. This is because it doesn't try to detect the proxy. The proxy detection process is described in Automatic Proxy Detection and involves several steps:

  1. 找到上次使用的代理配置脚本
  2. 从 DHCP 中发现代理
  3. 使用 DNS 查找名为 WAPD 的机器

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

Steps #2 and #3 can take a long time, depending on your network infrastructure and may even involve timeouts.

这篇关于新的 HttpClient 代理设置问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆