加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – DownloadStringAsync在第一次调用时阻塞线程14秒

发布时间:2020-12-15 08:24:05 所属栏目:百科 来源:网络整理
导读:这只发生在我的一台机器上.我认为这是一个环境配置问题.所有机器都运行ESET Smart Security软件防火墙.有任何想法吗? using System;using System.Net;using System.Diagnostics;using System.Threading;namespace Test{ static class Program { [STAThread]
这只发生在我的一台机器上.我认为这是一个环境配置问题.所有机器都运行ESET Smart Security软件防火墙.有任何想法吗?
using System;
using System.Net;
using System.Diagnostics;
using System.Threading;

namespace Test
{
    static class Program
    {
        [STAThread]
        static void Main()
        {
            bool exit = false;
            WebClient wc = new WebClient();
            DateTime before = DateTime.Now;
            wc.DownloadStringAsync(new Uri("http://74.125.95.147"),"First"); // IP Address of google,so DNS requests don't add to time.
            wc.DownloadStringCompleted += delegate(object sender,DownloadStringCompletedEventArgs e)
            {
                Debug.WriteLine(e.UserState + " Call: " + (DateTime.Now - before));

                if ((string)e.UserState == "First")
                {
                    before = DateTime.Now;
                    wc.DownloadStringAsync(new Uri("http://74.125.95.147"),"Second");
                }
                else
                    exit = true;
            };

            /*
             * 
             * Output:
             * 
             * First Call: 00:00:13.7647873
             * Second Call: 00:00:00.0740042
             * 
             */

            while (!exit)
                Thread.Sleep(1000);
        }
    }
}

解决方法

您的计算机配置为执行自动代理检测.

你可以在这里禁用它:

或者,您可以手动覆盖WebClient使用的代理.将WebClient.Proxy Property设置为null以指定不应使用代理.任何显式代理设置都会禁用自动代理检测.

client.Proxy = null;

但是,在这种情况下,您应该为用户提供在应用程序中配置代理的选项,因为某些用户在访问Web时需要使用代理.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读