连接Web Service所需的所有步骤(提供的WSDL和SOAP示例)

时间:2017-09-24 18:36:07

标签: c# web-services wsdl soapheader

我需要连接一个Web服务,这就是我拥有的所有信息:

  1. https://www.nameofthecompany.es:8443/webservices/functionIshouldcall?wsdl

  2. 电话示例:

    <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:env="http://address.provided.by.the.company.es"> <soapenv:Header> <wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"> <wsse:UsernameToken wsu:Id="UsernameToken-5"> <wsse:Username>Username</wsse:Username> <wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">Password</wsse:Password> </wsse:UsernameToken> </wsse:Security> </soapenv:Header> <soapenv:Body> <env:functionIshouldcall> <env:parameter1></env:parameter1> </env:functionIshouldcall> </soapenv:Body> </soapenv:Envelope>

  3. 我知道这个函数返回一个字符串;

  4. 这是我到目前为止所做的:

    • 创建一个服务引用,只添加第1点中给出的WSDL地址。

    • 创建了一个webservice实例,并使用所有需要的参数调用该函数但不是标头的用户和密码。

    我该怎么办? 提前谢谢。

1 个答案:

答案 0 :(得分:0)

如果您需要添加凭据,

This可能是一个很好的起点;我的猜测是你可能不得不,因为你以某种方式得到它们。 您添加凭据的部分如下所示:

UsernameToken userToken = new UsernameToken(userName, password, PasswordOption.SendHashed);
Service1 serviceProxy = new Service1();
SoapContext requestContext = serviceProxy.RequestSoapContext;
requestContext.Security.Tokens.Add(userToken);

简而言之:

  1. 通过将凭据嵌入特定令牌中来添加凭据 - 其类型属于Microsoft.Web.Services2.Security.Tokens命名空间
  2. 为您的服务创建代理(在上面的示例中为serviceProxy
  3. 通过您的服务RequestSoapContext
  4. 获取对其请求标头的访问权限
  5. 将令牌添加到请求
  6. 此外,我认为您可以跳过地址中的“?wsdl”部分,因为它引用了Web服务规范。 完成上述操作后,您可以尝试调用该函数并查看一切是如何工作的:如果函数必须返回某些内容,请检查它是否是您所期望的。

    当然不要忘记将您的代码放在try-catch块中,因为您可能需要检查一些异常并查看可能存在的错误。