如何获取引发 WebException 的 URI?
我正在调用 Web 服务上的方法,它抛出 403 Forbidden WebException...
System.Net.WebException:请求 失败,HTTP 状态为 403: 禁止。
我已记录此错误,但我真的希望将 URI 记录在日志消息中,以便轻松确定哪个 Web 服务导致了问题。
有没有一种简单的方法可以从抛出的 WebException 中获取 URI?我浏览了属性列表,但看不到任何可以满足我想要的东西。
I'm calling a method on a webservice and it is throwing a 403 Forbidden WebException...
System.Net.WebException: The request
failed with HTTP status 403:
Forbidden.
I've got this error logged but I'd really like to have the URI recorded in the log message so it is easy to determine which webservice is causing the problem.
Is there a simple way to get the URI from the WebException that is thrown? I've looked through the list of properties and I can't see anything that will get me what I want.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以访问 SOAP 客户端代理对象(
SoapHttpClientProtocol
类型)上的Url
属性。如果您从代码中的一个方法调用两个不同的 Web 服务,只需在 Web 服务调用周围放置一个 try {} catch 并使用有问题的 Web 服务的 Url 属性抛出适当的自定义
Exception
。像这样的东西:
You can access the
Url
property on your SOAP client proxy object (SoapHttpClientProtocol
type).If you're calling two different web services from one method in your code, simply put a try {} catch around the web service calls and throw an appropriate custom
Exception
with the Url property of the offending web service.Something like:
确保您没有通过需要身份验证的 Web 代理来调用该服务。您将收到此错误(或者可能是 401)错误,并绞尽脑汁(就像我所做的那样)试图找出故障发生的位置。我没有意识到发生了什么,直到我进行了网络跟踪,发现请求正在访问我们的出站身份验证代理,并且该请求没有凭据,然后被退回。
Be certain you are not making the call to the service through an authentication-required webproxy. You will get this (or perhaps a 401) error and pull your hair out (as I did) trying to figure out where the failure is occurring. I didn't realize what was happening until I did a network trace and found the requests were hitting our outbound authenticated proxy, and the request had no credentials, and bounced back.
这对我直接从 WebException 检索 Uri 很有用:
This worked for me to retrieve the Uri directly from a WebException: