(HttpWebResponse)request.GetResponse()挂起 [英] (HttpWebResponse)request.GetResponse() Hangs

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

问题描述

下面这行代码挂起。有没有办法在挂起时退出此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屋!

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