如何获取引发 WebException 的 URI?

发布于 2024-08-14 07:09:50 字数 278 浏览 3 评论 0原文

我正在调用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

各自安好 2024-08-21 07:09:50

您可以访问 SOAP 客户端代理对象(SoapHttpClientProtocol 类型)上的 Url 属性。

如果您从代码中的一个方法调用两个不同的 Web 服务,只需在 Web 服务调用周围放置一个 try {} catch 并使用有问题的 Web 服务的 Url 属性抛出适当的自定义 Exception

像这样的东西:

string url = client.Url;
try
{
  client.MyWebServiceCall();
  url = client2.Url;
  client2.MyWebServiceCall2();
}
catch (Exception ex)
{
  throw new Exception("Webservice call failed. Url: "+url+", Error:"+ex.Message,ex);
}

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:

string url = client.Url;
try
{
  client.MyWebServiceCall();
  url = client2.Url;
  client2.MyWebServiceCall2();
}
catch (Exception ex)
{
  throw new Exception("Webservice call failed. Url: "+url+", Error:"+ex.Message,ex);
}
寄意 2024-08-21 07:09:50

确保您没有通过需要身份验证的 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.

甩你一脸翔 2024-08-21 07:09:50

这对我直接从 WebException 检索 Uri 很有用:

Uri uri = wex.Response.ResponseUri;

This worked for me to retrieve the Uri directly from a WebException:

Uri uri = wex.Response.ResponseUri;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文