2013-03-07 69 views
1

我发送的POST和GET WebRequest应该支持更长的互联网停机时间。这个想法是对失败的(超时)Web请求进行排队,并尝试定期重新发送它们,直到互联网再次启动并发送所有排队的WebRequest。如何重新发送“失败”的WebRequest?

但是,我似乎不能只重用旧的WebRequest。我需要重新设置吗?

IAsyncResult result = request.BeginGetResponse (o => { 
     callCallback (o); 
}, state); 

当请求是仅使用设置:

var request = HttpWebRequest.Create (String.Format (@"{0}/{1}", baseServiceUrl, path)); 
request.Method = "GET"; 
request.ContentType = "application/xml; charset=UTF-8"; 
request.Headers.Add ("Authority", account.Session); 
return request; 

它工作正常。但超时后(和request.Abort();)和调用BeginGetResponse()在同一个webrequest只是冻结。

+1

可能重复? - http://stackoverflow.com/questions/2179626/reuse-a-httpwebrequest和另一个 - http://stackoverflow.com/questions/4933450/can-i-reuse-httpwebrequest-without-disconnecting-from-the-server – Snixtor 2013-03-07 10:25:33

+0

该问题不会调用'Abort()'来请求。 – Sunkas 2013-03-07 10:30:03

回答

1

您不能在同一HttpWebRequest上多次拨打BeginGetResponse()。我不确定这是否支持.NET,但是Mono的实现是不可能的(并且不是很容易改变的)。请参阅Can I reuse HttpWebRequest without disconnecting from the server? - 底层网络连接将被重新使用。

+1

谢谢。我通过创建一个围绕WebRequest的封装器来解决这个问题,该WebRequest存储用于设置请求的数据。这是为了能够重新创建它。 – Sunkas 2013-03-08 08:39:24

+0

我喜欢这些答案。我发现如何去做,但我不想和别人分享。 – Gregg 2016-12-05 02:08:05