Monday, 24 December 2007

How to change the version of HTTP that your .NET web-service client proxy class uses ...

.NET uses HTTP version 1.1 by default for HTTP traffic. There are a number of differences between HTTP 1.1 and HTTP 1.0, so if you are talking to a HTTP 1.0 web-service (which you will be if you are talking with a Java web-service), then your generated .NET client proxy class will not be able to successfully communicate. This is frustrating, given the fact that you can read and process the WSDL, and build a client against it, but then not use the web-service.

The easy work-around for this problem is to use a .NET 2.0+ Partial Class - as follows:

/***********************************************************************************************/

using System;
using System.Net;

public partial class YourProxyClassName
    {
        protected override System.Net.WebRequest GetWebRequest(Uri uri)
        {
            // set proxy config while we are in here
            WebRequest.DefaultWebProxy = null;

            System.Net.HttpWebRequest req;
            req = ( System.Net.HttpWebRequest)base.GetWebRequest(uri);

            req.ProtocolVersion = System.Net.HttpVersion.Version10;
            return req;
        }
    }

/***********************************************************************************************/

This code effectively overrides the version of HTTP that is used, replacing the default of version 1.1, with version 1.0.

No comments:

Post a Comment