1

I am writing an app to scrape my website for keywords. I am running into an issue where if I try to read a page that does not follow through I get a 404 (not a problem).

However when my app encounters the 404 web exception it does not continue. I know I need a try and a catch but i can't quite figure out how to add the 404 webexception catch properly without errors. Here is the code snippet with the issue: I think my problem is that I do not know where to properly put the try/catch as i have return source; I get an error that it is not returning a value when i add a catch.

string url = string.Format(@"http://127.0.0.1/website/" + searchterm);

HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(url);
req.Method = "GET";
req.UserAgent = "MoZilla/5.0 (Windows; U; MSIE 9.0; WIndows NT 9.0; en-US))";

Uri target = req.RequestUri;
string source;

using (StreamReader reader = new StreamReader(req.GetResponse().GetResponseStream()))
{
    source = reader.ReadToEnd();
}
return source;
  • possible duplicate of [How can I catch a 404?](http://stackoverflow.com/questions/1949610/how-can-i-catch-a-404) (Use the highest voted answer, not the accepted one) – Alex K. Aug 19 '15 at 13:24
  • Alex tried that method and couldn't figure out where to place the catch properly, searched stackoverflow for ages last night. :( – Jeremy Roberts Aug 19 '15 at 13:28
  • John Saunders answer is pretty much copy/paste/use whats the prob? – Alex K. Aug 19 '15 at 13:30
  • Since i have a return source; in there it seems like it don't want to use a try / catch, when i add a try or a catch to it, I receive a response similar to (no return value) i've added catch before and after "return source;" – Jeremy Roberts Aug 19 '15 at 13:30
  • the quick fix is to declare source=null at the top and have a single return it at the very end, if the calling code gets back a null it knows something went wrong. This would hide all possible errors tho so a better way is to return both the source buffer and an indication of success (as a ref/out parameter) – Alex K. Aug 19 '15 at 13:37
  • Now that is an answer. Thank you so much Alex!! I declared source at the top with a "" and catch worked properly. – Jeremy Roberts Aug 19 '15 at 13:46

1 Answers1

0

Thanks to Alex K:

I forgot to declare source = ""; at the top instead of bottom.

Thank you Alex K!

Community
  • 1
  • 1