[英]HttpWebResponse
I want to download images from the server. 我想从服务器下载图像。 When the image doesn't exist, I want to show my default image.
当图像不存在时,我想显示默认图像。
Here's my code: 这是我的代码:
string url = "http://www......d_common_conference" + "/" + c.id_common_conference + "-MDC.jpg";
try {
HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
request.Method = "HEAD";
HttpWebResponse response = request.GetResponse() as HttpWebResponse;
string status = Response.StatusCode.ToString();
img.ImageUrl = url;
}
catch (Exception excep) {
img.ImageUrl = "images/silhouete.jpg";
string msg = excep.Message;
}
It works nice, but until the 24th loop, no response, no exception thrown, and my program becomes jammed. 它工作正常,但是直到第24个循环,都没有响应,也没有引发异常,并且我的程序被卡住了。
How can I fix this? 我怎样才能解决这个问题?
You aren't disposing of the HttpWebResponse, try this instead: 您不需要处理HttpWebResponse,而是尝试以下方法:
HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
request.Method = "HEAD";
string status;
using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
{
status = response.StatusCode.ToString();
}
I suspect you've hit the limit on TCP connections your machine will make (can't remember the number, but it's per CPU if memory serves) 我怀疑您已经达到了机器将建立的TCP连接的限制(不记得这个数目,但是如果有内存,则是每个CPU)
ps there was a typo in your example, you weren't using the response
variable from your WebRequest, but the Response
object for the current request. ps在您的示例中有一个错字,您没有使用WebRequest中的
response
变量,而是使用了当前请求的Response
对象。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.