首页 新闻 会员 周边

使用 HttpWebResponse 接收 GET 的数据之后总是报“操作以超时”。

0
悬赏园豆:20 [已关闭问题] 关闭于 2014-05-08 15:13

在执行请求的时候经常会报错,提示“操作以超时”。

我已经在代码里处理了WebException异常。

GET 请求的方法:

 1         /// <summary>
 2         /// 执行HTTP GET请求。
 3         /// </summary>
 4         /// <param name="url">请求地址</param>
 5         /// <param name="parameters">请求参数</param>
 6         /// <returns>HTTP响应</returns>
 7         public static string DoGet(string url, IDictionary<string, string> parameters)
 8         {
 9             if (parameters != null && parameters.Count > 0)
10             {
11                 if (url.Contains("?"))
12                 {
13                     url = url + "&" + BuildPostData(parameters);
14                 }
15                 else
16                 {
17                     url = url + "?" + BuildPostData(parameters);
18                 }
19             }
20 
21             HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
22             req.ServicePoint.Expect100Continue = false;
23             req.Method = "GET";
24             req.KeepAlive = true;
25             req.UserAgent = "Test";
26             req.ContentType = "application/x-www-form-urlencoded;charset=utf-8";
27 
28             HttpWebResponse rsp = null;
29             try
30             {
31                 rsp = (HttpWebResponse)req.GetResponse();
32             }
33             catch (WebException webEx)
34             {
35                 if (webEx.Status == WebExceptionStatus.Timeout)
36                 {
37                     rsp = null;
38                 }
39             }
40 
41             if (rsp != null)
42             {
43                 if (rsp.CharacterSet != null)
44                 {
45                     Encoding encoding = Encoding.GetEncoding(rsp.CharacterSet);
46                     return GetResponseAsString(rsp, encoding);
47                 }
48                 else
49                 {
50                     return string.Empty;
51                 }
52             }
53             else
54             {
55                 return string.Empty;
56             }
57         }
查看代码

获取响应流的方法:

 1         /// <summary>
 2         /// 把响应流转换为文本。
 3         /// </summary>
 4         /// <param name="rsp">响应流对象</param>
 5         /// <param name="encoding">编码方式</param>
 6         /// <returns>响应文本</returns>
 7         private static string GetResponseAsString(HttpWebResponse rsp, Encoding encoding)
 8         {
 9             StringBuilder result = new StringBuilder();
10             Stream stream = null;
11             StreamReader reader = null;
12 
13             try
14             {
15 
16                 // 以字符流的方式读取HTTP响应
17                 stream = rsp.GetResponseStream();
18                 reader = new StreamReader(stream, encoding);
19 
20                 // 每次读取不大于256个字符,并写入字符串
21                 char[] buffer = new char[256];
22                 int readBytes = 0;
23                 while ((readBytes = reader.Read(buffer, 0, buffer.Length)) > 0)
24                 {
25                     result.Append(buffer, 0, readBytes);
26                 }
27             }
28             catch (WebException webEx)
29             {
30                 if (webEx.Status == WebExceptionStatus.Timeout)
31                 {
32                     result = new StringBuilder();
33                 }
34             }
35             finally
36             {
37                 // 释放资源
38                 if (reader != null) reader.Close();
39                 if (stream != null) stream.Close();
40                 if (rsp != null) rsp.Close();
41             }
42 
43             return result.ToString();
44         }
查看代码
Charles Zhang的主页 Charles Zhang | 初学一级 | 园豆:38
提问于:2014-04-10 18:16
< >
分享
所有回答(5)
0

先确定URL在浏览器里能不能打开

之后改   req.Timeout 设置超时值

cclient | 园豆:264 (菜鸟二级) | 2014-04-10 18:33
0

先确认一下服务端是否可以响应请求

贺臣 | 园豆:307 (菜鸟二级) | 2014-04-10 21:49

肯定是可以的,我是循环调用的百度的接口。

支持(0) 反对(0) Charles Zhang | 园豆:38 (初学一级) | 2014-04-10 21:51
0

循环调用的频率是多少?

一般API是会对调用频率进行限制以保证API的正常工作。

喵喵喵猫 | 园豆:1742 (小虾三级) | 2014-04-11 09:07

貌似没有吧,百度只是限制了请求次数10W每天

支持(0) 反对(0) Charles Zhang | 园豆:38 (初学一级) | 2014-04-11 10:37

@Charles Zhang: 试试修改本地连接数限制。

<system.net>
    <connectionManagement>
      <add address = "*" maxconnection = "512"/>
    </connectionManagement>
  </system.net>
支持(0) 反对(0) 喵喵喵猫 | 园豆:1742 (小虾三级) | 2014-04-11 10:53
0

应该是你循环太频繁了,百度API那边给你屏蔽了。

小胆粗心 | 园豆:300 (菜鸟二级) | 2014-04-11 10:33

我调用北京市的信息的时候几千条数据都没问题,为什么调用同样差不多数据量的上海却不行呢?

支持(0) 反对(0) Charles Zhang | 园豆:38 (初学一级) | 2014-04-11 10:35
0

频繁调用超时是很常见的,建议你添加重试的机制.

只会造轮子 | 园豆:2274 (老鸟四级) | 2014-04-11 12:58
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册