如何静默绕过失败的REST调用webRequest.GetResponse()(找不到404)? [英] How can I silently bypass a REST call to webRequest.GetResponse() that fails (404 Not Found)?

查看:118
本文介绍了如何静默绕过失败的REST调用webRequest.GetResponse()(找不到404)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我传递一个可识别的参数时,我的代码可以工作,但是当我传递的arg无法被识别时,我会收到一条错误消息.我不一定在每种情况下都期望(或需要)一个良好的响应,但是我不想生根发芽".没有结果可退给我的情况除外.

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.

我将举几个例子.如果我将此传递给(HttpWebRequest)WebRequest(使用"tt0003854"作为参数):

I'll give a couple of examples. If I pass this to (HttpWebRequest)WebRequest (using "tt0003854" as the arg):

https://api.themoviedb.org/3/movie/tt0003854/release_dates?api_key=Gr8GooglyMoogly&language=zh-CN&external_source=imdb_id

...我得到了我想要的东西:

...I get what I want back:

{"id":347745,结果":[{"iso_3166_1":"US","release_dates":[{"certification"":","iso_639_1": ","note":","release_date":"1936-12-12T00:00:00.000Z","type":3}]}]}}

{"id":347745,"results":[{"iso_3166_1":"US","release_dates":[{"certification":"","iso_639_1":"","note":"","release_date":"1936-12-12T00:00:00.000Z","type":3}]}]}

其他尝试也是如此.但是,有些失败,例如当我使用"tt0005929"时,作为arg:

And the same is true for other attempts. Some, though, fail, such as when I use "tt0005929" as the arg:

https://api.themoviedb.org/3/movie/tt0005929/release_dates?api_key=Gr8GooglyMoogly&language=zh-CN&external_source=imdb_id

...返回:

{成功":false,"status_code":34,"status_message":资源您 找不到所请求的内容.}

{"success":false,"status_code":34,"status_message":"The resource you requested could not be found."}

此行失败:

var webResponse = (HttpWebResponse)webRequest.GetResponse();

...然后掉到catch块,这时我得到一个错误消息,消息为:"远程服务器返回错误:(404)未找到"

...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"

没关系,如果找不到,但我不希望该应用程序因错误消息而停止.我该怎么做才能忽略"404"?

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);
}

推荐答案

第一个:强制转换或方法调用是否抛出错误? (只需将通话拆分为两行) 第二:您可以在该行中放置一个try {...} catch(Exception ex) /*{MessageBox.Show(Ex.Message)*/}块,但是,必须确保即使使用错误的值,以后的代码也可以工作,请确保对其进行测试.

First: Is the cast or the method call throwing the error? (Simply split the call and cast to two lines) Second: You can put a try {...} catch(Exception ex) /*{MessageBox.Show(Ex.Message)*/} block around the line, however, you must make sure that the later code works even with the wrong value, make sure to test it.

这篇关于如何静默绕过失败的REST调用webRequest.GetResponse()(找不到404)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆