我想使用以下代码使用Web服务:
WebService.GenRelClient client = new WebService.GenRelClient();
client.ClientCredentials.UserName.UserName = @"UserName";
client.ClientCredentials.UserName.Password = @"Password";
var response = client.returnString("test");
我的配置如下:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="GenRelClientPortBinding">
<security mode="TransportCredentialOnly">
<transport clientCredentialType="Basic" />
</security>
</binding>
</basicHttpBinding>
</bindings>
<client>
<endpoint
address="http://ws.domain.com/GenRel/GenRel"
binding="basicHttpBinding"
bindingConfiguration="GenRelClientPortBinding"
contract="WebService.GenRelClientPort"
name="GenRelClientPort" />
</client>
</system.serviceModel>
</configuration>
请求被发送到Web服务,并且回复时返回错误消息,指出它需要基本身份验证,因为请求是在没有凭据的情况下发送的,所以我不知道在哪里错误。
感谢您的帮助
答案 0 :(得分:10)