C# - HttpWebResponse redirect checker -


i'm trying code redirect checker, solution have banged morning it's not efficient need apart 1 thing:

it ever checks 2 sites before stopping, no errors occur, stops on "request.getresponse() httpwebresponse;" line third page.

i've tried using different sites , changing combination of pages check ever checks two.

any ideas?

        string urls = "/htmldom/default.asp/htmldom/dom_intro.asp/htmldom/dom_examples2.asp/xpath/default.asp";         string surl = "http://www.w3schools.com/";         string[] u = regex.split(urls, ".asp");          foreach (string site in u)         {             string superurl = surl + site + ".asp";              httpwebrequest request = (httpwebrequest)webrequest.create(superurl);              request.method = "head";             request.allowautoredirect = false;              var response = request.getresponse() httpwebresponse;             string = response.getresponseheader("location");              console.writeline("site: " + site + "\nresponse type: " + response.statuscode + "\nredirect page" + + "\n\n");         } 

aside fact break if webexception ever thrown, believe reason it's stopping never dispose of response. if have multiple urls served same site, they'll use connection pool - , not disposing of response, you're not releasing connection. should use:

using (var response = request.getresponse()) {     var httpresponse = (httpwebresponse) response;     // use httpresponse here } 

note i'm casting here instead of using as - if response isn't httpwebresponse, invalidcastexception on line more informative nullreferenceexception on next line...


Comments