How do I get the URI that threw a WebException?
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 an开发者_高级运维ything that will get me what I want.
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);
}
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.
This worked for me to retrieve the Uri directly from a WebException:
Uri uri = wex.Response.ResponseUri;
精彩评论