(HttpWebResponse)request.GetResponse()挂起 [英] (HttpWebResponse)request.GetResponse() Hangs
问题描述
下面这行代码挂起。有没有办法在挂起时退出此HTTP Web响应请求?
This line of code below hangs. is there a way to back out of this HTTP Web Response request when it hangs?
HttpWebResponse response =( HttpWebResponse )request.GetResponse();
HttpWebResponseresponse = (HttpWebResponse)request.GetResponse();
pianoboyCoder
pianoboyCoder
推荐答案
GetResponse是一个阻塞调用,直到服务器发送响应或请求超时。除非有充分的理由使用HttpWebRequest,否则不要使用它。这是相当低的水平。而是考虑使用
WebClient 或
HttpClient 。这两个都允许异步调用。
GetResponse is a blocking call until the server sends the response or the request times out. Unless there is a good reason for using HttpWebRequest then don't use it. It is pretty low level. Instead consider using WebClient or HttpClient. Both of these allow async calls.
对于异步调用,您可以随时使用
超时。如果等待超时,则可以使用已传递的CancellationToken取消请求(对于Httpclient)或调用CancelAsync(对于WebClient)。
For an async call you can wait however long you want with a timeout. If the wait times out then you can cancel the request using the CancellationToken that was passed (for Httpclient) or call CancelAsync (for WebClient).
Michael Taylor
http://www.michaeltaylorp3.net
Michael Taylor
http://www.michaeltaylorp3.net
这篇关于(HttpWebResponse)request.GetResponse()挂起的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!