Adding Custom Http Headers to Web Service Proxy

匿名 (未验证) 提交于 2019-12-03 01:56:01

问题:

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;     } }


易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!