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 this: 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?