I've got C# code that works when I pass a recognized imdb movieId, but when the movieId (tconst) that 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);
}
Can't find a movie or TV show? Login to create it.
Want to rate or add this item to a list?
Not a member?
Reply by Travis Bell
on September 28, 2020 at 11:12 AM
Hi @bclayshannon,
Unless someone familiar with C# happens to see this post, you probably won't get much action here. For coding specific issues like this, I usually recommend posting over on Stack Overflow.
Unfortunately I am not familiar with C# so I can't really point you in the right direction.