访问web服务上的方法时异常
本文关键字:方法 异常 web 服务 访问 | 更新日期: 2023-09-27 18:17:38
我正在编写一个客户端应用程序,它将使用web服务,并且应该能够获得数据。使用web服务的wsdl,我在客户机应用程序中生成了代理类。我遵循"添加服务引用"的方法来生成代理。现在,当我试图访问web服务上的方法时,我得到了这个错误:"既没有提供UsernameToken也没有提供签名"。我的第一个怀疑是它需要凭证,我试图在我的代码中提供它们。但我还是看到了这个问题。我在代码中提供凭据的原因是因为我们有2个web服务,一个是QA,另一个是生产。它们的wsdl相同,但凭据不同,因此我将使凭据可配置,以便最终用户可以根据他们想要访问的任何web服务输入它。
请让我知道,如果有什么我错过了。由于
下面是我的代码(这是一个测试webservice调用的小测试片段):GetApplicationServiceClient client= new GetApplicationServiceClient();
client.ClientCredentials.UserName.UserName = "userId";
client.ClientCredentials.UserName.Password = "password";
int[] input= new int[3]{1,2,3};
GetAppResponse response= new GetAppResponse();
foreach (var item in input)
{
response = client.getAppById(item);
}
这是我的配置文件:
<configuration>
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="GetApplicationServiceSoapBinding">
<security mode="Transport" />
</binding>
<binding name="GetApplicationServiceSoapBinding1" />
</basicHttpBinding>
</bindings>
<client>
<endpoint address="https://TestService.com/bo-service/v2/ws/get-application"
binding="basicHttpBinding" bindingConfiguration="GetApplicationServiceSoapBinding"
contract="TestService.GetApplicationService" name="GetApplicationServicePort" />
</client>
</system.serviceModel>
假设您的web服务提供基本身份验证的传输安全,请尝试
...
<binding name="GetApplicationServiceSoapBinding">
<security mode="Transport">
<transport clientCredentialType="Basic" />
</security>
</binding>
...