HttpWebRequest.GetResponse()不断超时 [英] HttpWebRequest.GetResponse() keeps getting timed out

查看:112
本文介绍了HttpWebRequest.GetResponse()不断超时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一个简单的C#函数,通过以下API调用从MtGox检索交易历史记录:

i wrote a simple C# function to retrieve trade history from MtGox with following API call:

https://data.mtgox.com/api/1/BTCUSD/trades?since=<trade_id>

在此处记录: https://en.bitcoin.it/wiki/MtGox/API/HTTP/v1#Multi_currency_trades

以下是函数:

string GetTradesOnline(Int64 tid)
{
    Thread.Sleep(30000);

    // communicate
    string url = "https://data.mtgox.com/api/1/BTCUSD/trades?since=" + tid.ToString();
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
    StreamReader reader = new StreamReader(response.GetResponseStream());

    string json = reader.ReadToEnd();
    reader.Close();
    reader.Dispose();
    response.Close();

    return json;
}

我从tid = 0(贸易编号)开始获取数据(从一开始就)。对于每个请求,我都会收到包含1000个交易详细信息的响应。我总是从下一个请求的前一个响应中发送交易ID。对于4个请求& ;;回应。但是之后,下面的行将引发 System.Net.WebException,并指出操作已超时:

i'm starting at tid=0 (trade id) to get the data (from the very beginning). for each request, i receive a response containing 1000 trade details. i always send the trade id from the previous response for the next request. it works fine for exactly 4 requests & responses. but after that, the following line throws a "System.Net.WebException", saying that "The operation has timed out":

HttpWebResponse response = (HttpWebResponse)request.GetResponse();

以下是事实:


  • 捕获异常并重新绑定会不断导致相同的异常

  • 默认的HttpWebRequest .Timeout和.ReadWriteTimeout已经足够高(超过一分钟)

  • 将HttpWebRequest.KeepAlive更改为false也不能解决任何问题

  • 即使功能失败,它似乎也始终在浏览器中工作

  • https://www.google.com
  • $检索响应没有问题。 b $ b
  • 例外之前的成功响应量每天都在变化(但浏览器始终可用)

  • 从上次失败的交易ID开始,立即导致例外

  • 从主线程调用此函数仍然导致异常

  • 在另一台计算机上运行该功能无效

  • 从其他IP运行它不起作用

  • 在请求之间增加Thread.Sleep并没有帮助

  • catching the exception and retying keeps causing the same exception
  • the default HttpWebRequest .Timeout and .ReadWriteTimeout are already high enough (over a minute)
  • changing HttpWebRequest.KeepAlive to false didn't solve anything either
  • it seems to always work in the browser even while the function is failing
  • it has no problems retrieveing the response from https://www.google.com
  • the amount of successful responses before the exceptions varies from day to day (but browser always works)
  • starting at the trade id that failed last time causes the exception immediately
  • calling this function from the main thread instead still caused the exception
  • running it on a different machine didn't work
  • running it from a different IP didn't work
  • increasing Thread.Sleep inbetween requests does not help

关于可能出问题的任何想法?

any ideas of what could be wrong?

推荐答案

我遇到了同样的问题。
对我来说,解决方法很简单,就像在using块中包装HttpWebResponse代码一样。

I had the very same issue. For me the fix was as simple as wrapping the HttpWebResponse code in using block.

using (HttpWebResponse response = (HttpWebResponse) request.GetResponse())
{
    // Do your processings here....
}

详细信息:当对同一主机发出多个请求时,通常会发生此问题,而 WebResponse 是处置不当。这就是 using 块将正确处理 WebResponse 对象,从而解决问题的地方。

Details: This issue usually happens when several requests are made to the same host, and WebResponse is not disposed properly. That is where using block will properly dispose the WebResponse object properly and thus solving the issue.

这篇关于HttpWebRequest.GetResponse()不断超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆