首页 新闻 会员 周边 捐助

HttpWebRequest Timeout失效

0
悬赏园豆:20 [已关闭问题] 关闭于 2015-11-11 17:45
using System;
using System.IO;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using HtmlAgilityPack;

namespace Test.Run
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            var client = new WebClient();
            client.Headers["User-Agent"] = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:40.0) Gecko/20100101 Firefox/40.0";
            System.Net.ServicePointManager.DefaultConnectionLimit = 5000;
            var html = client.DownloadString("http://www.xicidaili.com/nn/5");
            var doc = new HtmlDocument();
            doc.LoadHtml(html);


            HtmlNode tbody = doc.GetElementbyId("ip_list");
            if (tbody == null)
            {
                Console.WriteLine("xpath不正确");
                return;
            }
            int i = 0;

            foreach (var tr in tbody.ChildNodes)
            {
                if (tr.NodeType == HtmlNodeType.Element)
                {
                    i++;
                    if (i == 1)
                    {
                        continue;
                    }
                    var ip = tr.ChildNodes[5].InnerText;
                    var port = tr.ChildNodes[7].InnerText;

                    try
                    {
                        var request =
                         (HttpWebRequest)
                             WebRequest.Create("http://1111.ip138.com/ic.asp");
                        request.KeepAlive = false;
                       
                        request.Method = "get";
                        request.Timeout = 300;
                        request.ReadWriteTimeout = 300;
//                        request.ServicePoint.ConnectionLimit = 1000;
//                        request.ServicePoint.ConnectionLeaseTimeout = 1000;

                        request.ContentType = "application/json; charset=gb2312";


                        string address = ip + ":" + port;
                        Console.WriteLine(address);

                        request.Proxy = new WebProxy(address);

                        using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
                        {

                            using (Stream stream = response.GetResponseStream())
                            {
                                using (StreamReader sr = new StreamReader(stream, Encoding.GetEncoding("gb2312")))
                                {
                                    string result = sr.ReadToEnd();
                                    response.Close();
                                    Console.WriteLine(result);
                                }
                            }
                        }

                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                    }
                }
            }
        }
    }
}

检测代理是否有效时,设置的过期时间失去作用,控制台一只等待状态。

等待时长明显比设置时长大很多

问题补充:

设置了DefaultConnectionLimit,ServicePoint.ConnectionLimit 等属性还是一样

YangChengHu的主页 YangChengHu | 初学一级 | 园豆:11
提问于:2015-09-07 22:53
< >
分享
所有回答(3)
0

你这个不清楚没用过,用HttpClient吧,可以设置超时

zhoumy | 园豆:603 (小虾三级) | 2015-09-08 10:15
0

request.Timeout = 300; request.ReadWriteTimeout = 300;都设置了,也是在GetResponse()之前设置的,你也不是异步调用,说明你的超时设置是正确的,为什么时间不止是5秒,我想最有可能是:1你的DNS的解析时间,不包括在超时里面,2:你var request = (HttpWebRequest) WebRequest.Create("http://1111.ip138.com/ic.asp"),才计算超时时间;

稳稳的河 | 园豆:4216 (老鸟四级) | 2015-09-08 11:44
0

是否可以尝试 请求头中 添加 keepalive的东东

wolfy | 园豆:2636 (老鸟四级) | 2015-09-08 13:33
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册