views:

1027

answers:

2

I have an old application that uses the classic Web Service Proxy to interact with a Java Web Service. A while back the Web Service hoster decided to require a custom HTTP header to be sent with each request in order to access the service - otherwise the requests are thrown out outright (looks like this is some sort of router requirement). Regardless of what the reason I need to inject a custom HTTP header into the request.

Is there any way to interact with the actual Http client to do things like add custom headers?

A: 

Does this approach work?

great_llama
Unfortunately not. This is one of the scenarios (forced basic auth) but it's not working for me - the auth header is not sent without a challenge. This only works for auth anyway - I need a raw header.
Rick Strahl
+4  A: 

You should be able to do this by overriding the GetWebRequest method of the proxy class in a partial class in a separate file. After calling the base class method, you should be able to modify the returned HttpWebRequest however you like, then return it from the method:

public partial class MyServiceProxy {
    protected override WebRequest GetWebRequest(Uri uri) {
        HttpWebRequest request = (HttpWebRequest) base.GetWebRequest(uri);
        // do what you will with request.
        return request;
    }
}
John Saunders
Yup that does the trick. I'm bascially generating proxies via code and I was able to generate the class and inject some code to add headers by adding a Headers property that's checked and used in GetWebRequest to add additional headers. Ugly, but it works great!
Rick Strahl
thanks for the solution. How can I use this process with my web service client project?
Jack
@Jack: this is the web service client project. Also, you're better off using WCF if you have the choice.
John Saunders
@John I m developing client project too. How can I use this override process?
Jack
@Jack: the code above _is_ the client project. Use the code above.
John Saunders