MVC RESTFUL HTTPClient 尝试捕获异常
我有点困惑为什么我的 try...catch 异常似乎无法正常工作。我设置了以下工作正常的代码 - 调用 WCF 静态服务。我决定通过关闭 WCF Restful 服务来测试它,希望它会捕获错误消息或忽略 200(ok) 消息等,但由于某种原因..try catch 和 response.EnsureStatusIsSuccessful() 似乎被忽略,从而生成try catch 中间出现 HttpStageProcessingException 错误。
据我了解,当忽略 200 时,我应该只需要捕获 ArgumentOutOfRangeException...想法?
// set up passing parameters
var p = new HttpQueryString();
p.Add("username", username);
p.Add("password", password);
try
{
WebConfigurations cfg = new WebConfigurations();
HttpResponseMessage response;
// cfg - gets the base address for the URL of the WCF
using (HttpClient client = new HttpClient(cfg.GetBaseUrl()))
{
// exception HttpStageProcessingException occurs here...
response = client.Get(new Uri(client.BaseAddress + "MainService/ValidateUser"), p);
response.EnsureStatusIsSuccessful();
string memberallowed = string.Empty;
// process the resonse xml etc..NOT a problem...
ProcessStreamData proc = new ProcessStreamData();
memberallowed = proc.ReadStreamData(response);
// blah balh
return result;
}
}
catch (ArgumentOutOfRangeException aorEx)
{
Console.Write("Argument Out of Range: " + aorEx.Message);
return false;
}
// This really should not be here??? No error message is generated, too...
catch (HttpStageProcessingException timeEx)
{
Console.Write("Stage Processing" + timeEx.Message);
return false;
}
catch (Exception ex)
{
Console.Write("Standard error" + ex.Message);
return false;
}
I'm a little puzzled why my try...catch exception does not seem to work properly. I set up the following code which works fine - to call a WCF restful service. I decided to test it by shutting off the WCF restful service in hopes that it will trap error messages or ignore a 200(ok) message etc but for some reason..the try catch and response.EnsureStatusIsSuccessful() seems to be ignored thus generating a HttpStageProcessingException error in the middle of the try catch..
As I understand it, I should only need to catch the ArgumentOutOfRangeException when a 200 is ignored...thoughts?
// set up passing parameters
var p = new HttpQueryString();
p.Add("username", username);
p.Add("password", password);
try
{
WebConfigurations cfg = new WebConfigurations();
HttpResponseMessage response;
// cfg - gets the base address for the URL of the WCF
using (HttpClient client = new HttpClient(cfg.GetBaseUrl()))
{
// exception HttpStageProcessingException occurs here...
response = client.Get(new Uri(client.BaseAddress + "MainService/ValidateUser"), p);
response.EnsureStatusIsSuccessful();
string memberallowed = string.Empty;
// process the resonse xml etc..NOT a problem...
ProcessStreamData proc = new ProcessStreamData();
memberallowed = proc.ReadStreamData(response);
// blah balh
return result;
}
}
catch (ArgumentOutOfRangeException aorEx)
{
Console.Write("Argument Out of Range: " + aorEx.Message);
return false;
}
// This really should not be here??? No error message is generated, too...
catch (HttpStageProcessingException timeEx)
{
Console.Write("Stage Processing" + timeEx.Message);
return false;
}
catch (Exception ex)
{
Console.Write("Standard error" + ex.Message);
return false;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在告诉 Visual Studio 不要因抛出的异常而中断后,您是否尝试过执行此操作?从菜单中,“调试”->“调试”然后取消选中抛出异常。
另外,您可能想从 Rest Starter Kit 中转储该版本的 HttpClient。最新版本可以在这里找到 http://nuget.org/List/Packages/HttpClient 和请注意,有重大变化。
Have you tried doing this after telling Visual Studio not to break on thrown exceptions? From the Menu, Debug-> Exceptions and then uncheck thrown.
Also, you probably want to dump that version of HttpClient from the Rest Starter Kit. The latest version can be found here http://nuget.org/List/Packages/HttpClient and be warned there are breaking changes.