Quantcast
Channel: August 2004 - Buck Hodges
Viewing all articles
Browse latest Browse all 5

Handling HTTP response codes in SOAP proxies

$
0
0

Continuing with the last post on authentication issues with wsdl.exe-generated SOAP proxies, you may need to handle HTTP status codes to provide custom exceptions.  To do this, the SoapHttpClientProtocol class (actually its parent HttpWebClientProtocol) provides a pair of GetWebResponse() methods for derived classes to override.  One method is for synchronous calls and the other for asynchronous.

The following code shows how to generate a custom exception for the HTTP 401 Unauthorized code.

using System;
using System.Net;

public class MyProxy : WsdlExeGeneratedProxy
{
   protected override WebRequest GetWebRequest(Uri uri)
   {
      HttpWebRequest request = base.GetWebRequest(uri) as HttpWebRequest;
      request.Headers.Add("accept-language", CultureInfo.CurrentUICulture.Name);
      request.Credentials = m_state.Credentials;
      return request;
   }
   protected override WebResponse GetWebResponse(WebRequest request)
   {
      HttpWebResponse response = base.GetWebResponse(request) as HttpWebResponse;
      ThrowIfUnauthorized(response);
      return response;
   }
   protected override WebResponse GetWebResponse(WebRequest request, IAsyncResult result)
   {
      HttpWebResponse response = base.GetWebResponse(request, result) as HttpWebResponse;
      ThrowIfUnauthorized(response);
      return response;
   }
   private void ThrowIfUnauthorized(HttpWebResponse response)
   {
      // There are cases where response can be null (dropped network connection is one).
      if (response != null && response.StatusCode == HttpStatusCode.Unauthorized)
      {
         throw new UnauthorizedException(response);
      }
   }
   internal SomeObject m_state;
}
public class UnauthorizedException : ApplicationException
{
   public UnauthorizedException(HttpWebResponse response)
   {
      m_response = response;
   }
   internal HttpWebResponse m_response;
}

The post Handling HTTP response codes in SOAP proxies appeared first on Buck Hodges.


Viewing all articles
Browse latest Browse all 5

Trending Articles