开发者

.Net WebRequest Fails all the time after first failure

Okay so here is a random error. I'm dynamically adding images to an html page. The images are on another server that I do not have control of. The images are named something like this: imageName10.jpg imageName11.jpg imageName13.jpg imageName14.jpg imageName16.jpg imageName17.jpg imageName19.jpg

Lets take the list of images above. I want to get all the images available, but I do not know how many there are. I do know that it starts with the string "imageName", has a number between 10 and 20 and then ends with the string ".jpg". I decided to create a loop from 10 to 20 creating the image name and adding the image tags to my html. This works, however I do not want to add broken links so I want to first check if the image exists. To do this I used a WebRequest to get the image url with a timeout of 5 seconds. If it times out it basically skips over the link and continues the loop.

        String dynamicHtmlStr = String.Empty;
        dynamicHtmlStr += "<TABLE>";
        WebRequest webReq = null;
        WebResponse webResp = null;
        for (int i = 10; i < 20; i++)
        {
            try
            {
                webReq = WebRequest.Create("http://www.someurl.com/image/imageName" + i + ".jpg");
                webReq.Timeout = 5000;
                webResp = webReq.GetResponse();

                dynamicHtmlStr += "<TR>";
                dynamicHtmlStr += "<TD>";
                dynamicHtmlStr += "<IMG http://www.someurl.com/image/imageName" + i + ".jpg"\">";
                dynamicHtmlStr += "</TD>";
                dynamicHtmlStr += "</TR>";
            }
            catch (Exception)
            {
            }
        }
        dynamicHtmlStr += "</TABLE>";
        return dynamicHtmlStr;

My issue is thi开发者_Go百科s: After the webrequest fails the first time. all other web requests after that seem to fails too. For instance, take the image list above. The loop starts at 10. it does a web request to see if "imageName10.jpg" exists, gets a good response, I add the html string to the dynamicHtmlStr variable. It loops again, this time on 11 and eveything is still fine. Then we move on to 12. 12 is false and does not add to the dynamicHtmlStr because it does not exists, which is correct. We move onto 13 which does exist, but this too fails and every other web request after that, regardless of if it exists or not.

This makes no sense to me. Am I doing something wrong?


The situation is a bit odd, I must say.

I tend to use the HttpWebRequest / HttpWebResponse classes with great success directly with the streams.

The request is quite straightforward, have a look here and here.

Once you have created your request, you can look at the response here:

using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
    using (Stream responseStream = response.GetResponseStream())
    {
        using (StreamReader readStream = new StreamReader(responseStream, Encoding.UTF8))
        {
           // Put in code to check response status. You'll probably get a 404 when not found.
        }
    }
}

Another note: I know that the loop size is minute, but you should always try to use a StringBuilder for iterative string concatenation.


I've encountered the same issue.

According to sample of this link: http://msdn.microsoft.com/en-us/library/system.net.webrequest.begingetresponse.aspx

Place a response.GetResponseStream().Close(); before a response.Close(); or before the end of using segment, could possibly solve the issue.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜