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

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

问题描述

我试图根据新的固件4.5功能,如HttpClient的改写旧网络身份验证逻辑,并等待/异步和我遇到的请求和响应之间意外的延迟(约15秒)。我的猜测是,是因为客户端试图从IE /使用代理服务器就像是旧型Htt prequest / Web客户端。这里的code:

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;
}

认证发生在相应的视图模型命令:

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

当我去掉=并将useProxy虚假HttpClientHandler设置,延迟去世和身份验证有没有延迟。问题是,有时同样的情况,即使我不并将useProxy取消注释(每个约十几运行一个运行)。现在的问题是 - 这是一个bug还是什么?试图调试,从服务器端,查询请求之间没有差异发现。先谢谢了。

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的与Web客户端都使用系统的默认设置,除非您覆盖它们。

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 DISCOVER代理
  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天全站免登陆