Doppelter Aufruf von HttpWebRequest.GetResponse führt zu Exception
-
Folgender Code reproduziert mein Problem:
public void Download() { try { string destinationFolder = @"C:\UnitTest\"; if (!Directory.Exists(destinationFolder)) { Directory.CreateDirectory(destinationFolder); } string url = @"http://www.c-plusplus.net/cms/images/logo.gif"; // Download boost // Check if URL is valid checkUrl(url); checkUrl(url); System.Net.WebClient _WebClient = new System.Net.WebClient(); // Downloads the resource with the specified URI to a local file. _WebClient.DownloadFile(url, @"C:\UnitTest\logo.gif"); } catch (System.Exception ex) { Assert.True(false); } } private void checkUrl(string url) { //Creating the HttpWebRequest HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest; //Setting the Request method HEAD, you can also use GET too. request.Method = "HEAD"; //Getting the Web Response. HttpWebResponse response = request.GetResponse() as HttpWebResponse; //Returns TURE if the Status code == 200 Assert.True(response.StatusCode == HttpStatusCode.OK); }
Rufe ich checkUrl(url); nur einmal auf wird das Bild heruntergeladen - rufe ich es zweimal auf dann nicht. Hat jemand eine Idee warum das so ist?
-
Schnelle Vermutung: Füge am Ende von
checkUrl
noch einresponse.Close()
ein.Grüssli
-
genau das war der Fehler - danke