Search code examples
c#.netweb-servicesurisystem.net.webexception

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 anything that will get me what I want.


Solution

  • 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);
    }