问题
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?
回答1:
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;
}
}
来源:https://stackoverflow.com/questions/1309767/adding-custom-http-headers-to-web-service-proxy