我有一个控制台应用程序,只需单击写入并使用“添加服务引用”添加Web引用,然后将我的.Config文件更改为:
<?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>
所以,一切都很好,似乎我可以成功使用该服务,但服务文档说我需要在请求的标题上设置用户名和密码,这是文档的示例:
<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>
那么如何在标题中添加用户名和密码?有什么建议吗?
答案 0 :(得分:2)
对于添加静态标头,只需添加.config文件端点部分即可将端点更改为:
<client>
<endpoint address="http://ServiceAdd/FService" binding="basicHttpBinding"
bindingConfiguration="FServiceSoapBinding" contract="MyReference.MService_"
name="FServicePort" >
<headers>
<Account>
<username>user</username>
<password>password</password>
</Account>
</headers>
</endpoint>
</client>
答案 1 :(得分:0)