如何使用auth标头访问webservice

时间:2015-06-03 08:53:47

标签: c# web-services soap asmx soapheader

如何访问此Web服务。我是这个领域的新手。我有一个具有认证功能的服务。请帮我看一下。

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Header>
    <MQUserNameToken xmlns="http://tempuri.org/">
      <User_id>string</User_id>
      <Password>string</Password>
      <ExternalPartyName>string</ExternalPartyName>
    </MQUserNameToken>
  </soap:Header>
  <soap:Body>
    <GetCustomerInfo xmlns="http://tempuri.org/">
      <CustomerInfoXML>string</CustomerInfoXML>
      <ReferenceNo>string</ReferenceNo>
    </GetCustomerInfo>
  </soap:Body>
</soap:Envelope>

1 个答案:

答案 0 :(得分:0)

配置你信封的headerOut

    envelope.headerOut= new Element[1];
    envelope.headerOut[0] = buildAuthHeader(user_id,pwd,externalPartyName);

定义buildAuthHeader方法如下

private Element buildAuthHeader(String user_id, String pwd, String externalPartyName) {

        Element h = new Element().createElement(NAMESPACE,"MQUserNameToken");
        Element username = new Element().createElement(NAMESPACE, "User_id");   
        username.addChild(Node.TEXT,user_id);
        h.addChild(Node.ELEMENT, username);

        Element password = new Element().createElement(NAMESPACE, "Password");
        password.addChild(Node.TEXT,pwd);
        h.addChild(Node.ELEMENT, password);

        Element externalParty= new Element().createElement(NAMESPACE, "ExternalPartyName");
        pass.addChild(Node.TEXT,externalPartyName);
        h.addChild(Node.ELEMENT, ExternalPartyName);
        }
        return h;
}