I've got code that works when I pass a recognized argument, but when the arg I pass is not recognized, I get an error message. I don't necessarily expect (or need) a good response in every case, but I don't want to "bothered" by an exception when there are no results to return to me.
I'll give a couple of examples. If I pass this to (HttpWebRequest)WebRequest (using "tt0003854" as the arg):
...I get what I want back:
{"id":347745,"results":[{"iso_3166_1":"US","release_dates":[{"certification":"","iso_639_1":"","note":"","release_date":"1936-12-12T00:00:00.000Z","type":3}]}]}
And the same is true for other attempts. Some, though, fail, such as when I use "tt0005929" as the arg:
...which returns:
{"success":false,"status_code":34,"status_message":"The resource you requested could not be found."}
It fails on this line:
var webResponse = (HttpWebResponse)webRequest.GetResponse();
...and falls to the catch block, at which point I get an err msg that says, "The remote server returned an error: (404) Not Found"
It's "okay" if it's not found, but I don't want the app to be stopped by an error message. What can I do to ignore the "404"s?
Here is more of my code, for context:
try
{
var webRequest = (HttpWebRequest)WebRequest.Create(RESTStringToGetMPAARatingForMovieId);
webRequest.Method = "GET"; // <-- GET is the default method/verb, but it's here for clarity
var webResponse = (HttpWebResponse)webRequest.GetResponse();
if (webResponse.StatusCode == HttpStatusCode.NotFound)
{
continue; // this is not reached, even when I get the error
}
if ((webResponse.StatusCode != HttpStatusCode.OK) || (webResponse.ContentLength == 0))
{
continue; // this is not reached, either
}
if ((webResponse.StatusCode == HttpStatusCode.OK) && (webResponse.ContentLength > 0))
{
StreamReader streamReader = new StreamReader(webResponse.GetResponseStream());
string s = streamReader.ReadToEnd();
. . .
}
else
{ // I don't see this message
MessageBox.Show(string.Format("Status code == {0}, Content length == {1}",
webResponse.StatusCode, webResponse.ContentLength));
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}