向简单 Web 服务请求添加标头 [英] Add Header to the Simple Web Service Request
问题描述
我有一个控制台应用程序,我只需通过写入单击并使用添加服务引用"来添加 Web 引用,然后我的 .Config 文件更改为:
I have a Console application, I add a Web reference simply by write click and use "Add Service Reference", then my .Config file changed to:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="FServiceSoapBinding" />
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://ServiceAdd/FService" binding="basicHttpBinding"
bindingConfiguration="FServiceSoapBinding" contract="MyReference.MService_"
name="FServicePort" />
</client>
</system.serviceModel>
</configuration>
所以,一切都很好,似乎我可以成功使用该服务,但是服务文档说我需要在请求的标头上设置用户名和密码,这是文档示例:
So, every thing is fine and it seems I can use the service successfully, but the service documentation said I need set username and password on the header of the requests, and this is the example of the documentation:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:web="http://namespace.com/">
<soapenv:Header>
<Account>
<username>user</username>
<password>password</password>
</Account>
</soapenv:Header>
<soapenv:Body>
<web:oneofservicemethods>
<items>
<item>...</item>
</items>
</web:oneofservicemethods>
</soapenv:Body>
</soapenv:Envelope>
那么如何将用户名和密码添加到标题中?有什么建议吗?
So How can I add username and password to the header? any suggestion?
推荐答案
如果我理解正确,您想在请求的标题上设置用户名和密码.您可以通过将 WCF Extensibility – Message Inspectors
与 IClientMessageInspector.BeforeSendRequest
结合使用来实现.
If i understand correctly, you want to set username and password on the header of the requests. You can achieve by using WCF Extensibility – Message Inspectors
with IClientMessageInspector.BeforeSendRequest
.
创建一个实现 IClientMessageInspector
的类.在 BeforeSendRequest 方法中,将您的自定义标头添加到传出消息中.
Create a class that implements IClientMessageInspector
. In the BeforeSendRequest method, add your custom header to the outgoing message.
这篇关于向简单 Web 服务请求添加标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!