使用WCF客户端访问RESTful服务时访问HTTP状态代码

编程入门 行业动态 更新时间:2024-10-22 13:41:36
本文介绍了使用WCF客户端访问RESTful服务时访问HTTP状态代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

感谢此答案,现在,我可以使用WCF客户端成功调用JSON RESTful服务.但是该服务使用HTTP状态代码来通知结果.我不确定如何访问这些状态代码,因为我在调用服务时仅在客户端收到异常.即使该异常也不具有HTTP状态代码属性.它只是埋在异常消息本身中.

Thanks to this answer, I am now able to successfully call a JSON RESTful service using a WCF client. But that service uses HTTP status codes to notify the result. I am not sure how I can access those status codes since I just receive an exception on client side while calling the service. Even the exception doesn't have HTTP status code property. It is just buried in the exception message itself.

问题是,在调用服务时如何检查/访问响应的HTTP状态代码.

So the question is, how to check/access the HTTP status code of response when the service is called.

推荐答案

作为一个捷径,您可以访问状态代码,如下所示:

As a quick win, you can access the status code in the exception like this:

try { client.DoSomething(); // call the REST service } catch (Exception x) { if (x.InnerException is WebException) { WebException webException = x.InnerException as WebException; HttpWebResponse response = webException.Response as HttpWebResponse; Console.WriteLine("Status code: {0}", response.StatusCode); } }

也许有一个带有消息检查器的解决方案.但是我还没有弄清楚.

Maybe there's a solution with a message inspector. But I haven't figured it out yet.

更多推荐

使用WCF客户端访问RESTful服务时访问HTTP状态代码

本文发布于:2023-11-25 09:11:33,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1629173.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:客户端   状态   代码   WCF   HTTP

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!