如何为使用 Axis 1.4 Web 服务的 C# Web 服务客户端添加自定义 Http 标头
我正在尝试用 C# 编写一个 Web 服务客户端,其中 Web 服务是 Java Axis 1.4。 Axis 服务需要 HTTP 标头中的 Authorization: Basic Base64EncodedToken 标头值。 我找不到在 Visual Studio.net 中使用 Web 服务的标准方式设置此标头的方法,例如正常的 WSDL 生成的引用,也无法使用 WSE3.0
我无法使用 WCF,因为该项目是使用 .net 2.0 开发的。
有什么办法可以做到这一点吗?
I'm trying to write a web service client in c# which the webservice is Java Axis 1.4.
Axis service requires the Authorization: Basic Base64EncodedToken header value in the HTTP Headers.
I can't find a way to set this header in standart ways of consuming web services in visual studio.net, like normal WSDL generated refernce nor with WSE3.0
I can't use WCF as the project is developed using .net 2.0.
Is there any way to do this ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
user334291 的回答对我来说是救星。 只是想添加如何添加 OP 最初打算执行的操作(我最终使用的内容):
在生成的 Web 服务代码上覆盖 GetWebRequest 函数:
并在调用 Web 服务之前设置凭据:
user334291's answer was a life saver for me. Just want to add how you can add what the OP originally intended to do (what I ended up using):
Overriding the GetWebRequest function on the generated webservice code:
and setting the credentials before calling the webservice:
这对我有用:
不要忘记设置此属性:
Cookie 和 Cookie2 在标头中设置,因为 java 服务不接受请求,并且我收到未经授权的错误。
Here is what worked for me:
Don't forget to set this property:
Cookie and Cookie2 are set in header because java service was not accepting the request and I was getting Unauthorized error.
看来原作者已经找到了他们的解决方案,但对于任何其他想要添加实际自定义标头的人来说,如果您有权修改生成的协议代码,您可以覆盖
GetWebRequest
:请确保删除如果您想单步执行,请使用
DebuggerStepThroughAttribute
属性。It seems the original author has found their solution, but for anyone else who gets here looking to add actual custom headers, if you have access to mod the generated Protocol code you can override
GetWebRequest
:Make sure you remove the
DebuggerStepThroughAttribute
attribute if you want to step into it.我们在这里谈论 WCF 吗? 我遇到了服务调用未添加 http 授权标头的问题,将任何调用包装到此语句中解决了我的问题。
这是通过 .NET 对 IBM ESB 运行 SOAP 调用,并通过 http 或 https 进行基本身份验证。
我希望这可以帮助别人,因为我在网上寻找解决方案时遇到了很多问题。
Are we talking WCF here? I had issues where the service calls were not adding the http authorization headers, wrapping any calls into this statement fixed my issue.
This was running SOAP calls to IBM ESB via .NET with basic auth over http or https.
I hope this helps someone out because I had massive issues finding a solution online.
您可以通过添加自定义消息检查器来注入自定义 HTTP 标头,而不是修改自动生成的代码或将每个调用包装在重复的代码中,这比听起来更容易:
实例化客户端类时,您可以简单地将其添加为行为:
这将使每个传出服务调用都具有您的自定义 HTTP 标头。
Instead of modding the auto-generated code or wrapping every call in duplicate code, you can inject your custom HTTP headers by adding a custom message inspector, it's easier than it sounds:
And when instantiating your client class you can simply add it as a behavior:
This will make every outgoing service call to have your custom HTTP header.
如果您想发送自定义 HTTP 标头(而不是 SOAP 标头),那么您需要使用 HttpWebRequest 类,代码如下所示:
您无法使用 Visual Studio 生成的代理添加 HTTP 标头,这可能是一个真正的痛苦。
If you want to send a custom HTTP Header (not a SOAP Header) then you need to use the HttpWebRequest class the code would look like:
You cannot add HTTP headers using the visual studio generated proxy, which can be a real pain.
我找到这段代码并解决了我的问题。
http://arcware.net/setting-http-header-authorization-用于网络服务/
I find this code and is resolve my problem.
http://arcware.net/setting-http-header-authorization-for-web-services/