Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding Custom Http Headers to Web Service Proxy

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?

like image 467
Rick Strahl Avatar asked Aug 21 '09 02:08

Rick Strahl


People also ask

Can you add custom HTTP headers?

In the Add Custom HTTP Response Header dialog box, set the name and value for your custom header, and then click OK.

How do I add HTTP headers to my website?

Select the web site where you want to add the custom HTTP response header. In the web site pane, double-click HTTP Response Headers in the IIS section. In the actions pane, select Add. In the Name box, type the custom HTTP header name.

What are proxy headers?

The HTTP Proxy-Authorization request header contains the credentials to authenticate a user agent to a proxy server, usually after the server has responded with a 407 Proxy Authentication Required status and the Proxy-Authenticate header.

What are custom headers in API?

Custom Headers allow us to add extra content to our HTTP requests and responses, which we can pass between the client and server. We can use custom headers for metadata, such as defining the current version of the API that is being used.


1 Answers

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;
    }
}
like image 196
John Saunders Avatar answered Oct 10 '22 23:10

John Saunders