HttpWebRequest.GetResponse 错误 time out

reallyioio 2010-07-29 03:04:03
使用HttpWebRequest.GetResponse方法来获取网页内容,但是却经常发生time out 错误
代码如下:

Dim aResponse As HttpWebResponse
Dim aRequest As HttpWebRequest = WebRequest.Create(strURL)
aRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)"
aRequest.Method = "GET"
aRequest.Timeout = 10000
aRequest.PreAuthenticate = True
aRequest.AllowAutoRedirect = False

Try
aResponse = aRequest.GetResponse()
Console.WriteLine("Request done. ")
Catch ex As Exception
Console.WriteLine("Request error. " & ex.Message)
Exit Sub
End Try

说明: 这段代码是要轮询多次执行的,因为会在strURL中附加不同的参数传递过去,然后拿到不同的结果,但是经常发生time out 错误。
我把strURL 设置成 “http://www.sohu.com”也不行,一共轮询执行了5次,只有前2次成功,后面的全部都是time out错误。这个是什么原因啊,是不是在网站一方设置了限制,防止同一ip地址同一时间多次访问,防止攻击?
我对。net不太熟悉,各位给点意见啊,谢了先
...全文
89 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
dgl_1225 2010-09-24
  • 打赏
  • 举报
回复
你的超时定的是10000毫秒,也就是10秒,有点短
兔子-顾问 2010-09-15
  • 打赏
  • 举报
回复
加个try,catch,如果timeout了。重新请求。我就这么做的。不知道有没更好办法。
wuyq11 2010-09-15
  • 打赏
  • 举报
回复
System.Net.WebClient wc = new System.Net.WebClient();
wc.Credentials = System.Net.CredentialCache.DefaultCredentials;
Byte[] pageData = wc.DownloadData(PageUrl);
string Content= System.Text.Encoding.Default.GetString(pageData);
定时timer重新请求
pova 2010-07-29
  • 打赏
  • 举报
回复
也不熟,帮顶~
简单C#信息采集工具实现 http://blog.csdn.net/xiaoxiao108/archive/2011/06/01/6458367.aspx 最近想整只爬虫玩玩,顺便熟悉下正则表达式。 开发环境 vs2008 sql2000 实现方法如下 1.先抓取网页代码 2.通过正则匹配出你需要的内容 比如http://www.soso.com/q?w=%C4%E3%BA%C3&pg=1 页面中 搜索结果的标题跟连接地址。具体可以根据你的需要填写合适的地址跟正则。 3.把匹配出的内容保存到数据库中。对其中的数据可以根据需要自己进行处理 具体实现代码 1.读取网页的代码 public static string GetDataFromUrl(string url) { string str = string.Empty; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); //设置Http头; request.AllowAutoRedirect = true; request.AllowWriteStreamBuffering = true; request.Referer = ""; request.Timeout = 10 * 1000; //request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022)"; HttpWebResponse response = null; try { response = (HttpWebResponse)request.GetResponse(); if (response.StatusCode == HttpStatusCode.OK) { //根据http应答头来判别编码 string Characterset = response.CharacterSet; Encoding encode; if (Characterset != "") { if (Characterset == "ISO-8859-1") { Characterset = "gb2312"; } encode = Encoding.GetEncoding(Characterset); } else { encode = Encoding.Default; } //声明一个内存流来贮存http应答流 Stream Receivestream = response.GetResponseStream(); MemoryStream mstream = new MemoryStream(); byte[] bf = new byte[255]; int count = Receivestream.Read(bf, 0, 255); while (count > 0) { mstream.Write(bf, 0, count); count = Receivestream.Read(bf, 0, 255); } Receivestream.Close(); mstream.Seek(0, SeekOrigin.Begin); //从内存流里读取字符串这里涉及到了编码方案 StreamReader reader = new StreamReader(mstream, encode); char[] buf = new char[1024]; count = reader.Read(buf, 0, 1024); while (count > 0) { str += new string(buf, 0, 1024); count = reader.Read(buf, 0, 1024); } reader.Close(); mstream.Close(); } } catch (Exception ex) { GetDataFromUrl(url); } finally { if (response != null) response.Close(); } return str; } 2.正则匹配的代码 public static ArrayList GetString(string reg, string content) { Regex r = new Regex(reg, RegexOptions.Compiled); MatchCollection matches = r.Matches(content); ArrayList a = new ArrayList(); foreach (Match m in matches) { string[] arr = new string[10]; arr[0] = m.Groups[1].Value; arr[1] = m.Groups[2].Value; arr[2] = m.Groups[3].Value; arr[3] = m.Groups[4].Value; arr[4] = m.Groups[5].Value; arr[5] = m.Groups[6].Value; arr[6] = m.Groups[7].Value; arr[7] = m.Groups[8].Value; arr[8] = m.Groups[9].Value; arr[9] = m.Groups[10].Value; a.Add(arr); } return a; } 3.如果抓取的页面很多 ,可以把多线程跟队列应用过来,提高抓取效率 Queue numbers = new Queue(); const int MaxCount = 5;//同时运行的最多线程数 private static object _lock = new object(); private void Test() { while (true) { int i = 0; lock (_lock) { if (numbers.Count == 0) { flag = false; return; } i = numbers.Dequeue(); } f(i); } } void Ssss() { for (int i = 1; i <= 100; i++)//处理的页面参数 从http://www.soso.com/q?w=你好&pg=1 到http://www.soso.com/q?w=你好&pg=100 { numbers.Enqueue(i); } for (int i = 0; i < MaxCount; i++) { Thread thread = new Thread(new ThreadStart(Test)); thread.Name = "T" + i.ToString(); thread.Start(); } } private void f(int num) { string str = ClassLibrary1.Class1.GetDataFromUrl("http://www.soso.com/q?w=%C4%E3%BA%C3&pg="+num); string reg = "]+? target=\"_blank\">([\\s\\S]+?)"; ArrayList a = ClassLibrary1.Class1.GetString(reg, str); for (int i = 0; i ] 除了>以为的字符 [\u4e00-\u9fa5] 汉字 6.代码只是实现了信息采集的主要功能,根据你自己的需要更换采集页面,跟合适的正则表达式后,可以根据你的需要自动进行采集,对采集到的数据,再根据你的需要自己进行处理。 7.数据库操作部分用的3层代码生成器连接地址 在 app.config中 如果你发现有什么不合理的,需要改进的地方,联系328452421@qq.com 朱晓 。相互交流 谢谢 顺便问下 有家是新泰的没,搞软件开发 地

16,554

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧