歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux編程 >> Linux編程

Java通過axis調用.NET WebService

前面已經記錄過通過axis調用webservice《http://www.linuxidc.com/Linux/2015-06/118460.htm》,昨天在對接.NET接口時,發現在傳遞參數時和一般的傳參方法是不一樣的,在接口方哥們的幫助下解決了。哈哈。

import java.net.URL;
import javax.xml.namespace.QName;
import javax.xml.rpc.ParameterMode;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.encoding.XMLType;
import org.apache.axis.message.SOAPHeaderElement;
public class MapWebServices {
 public static void main(String[] args) {
  String soapaction = "http://xxxx.xxxx.xxx/xsd";
  String endpoint = "http://xx.xxx.xx.xxx:8080/XxxXxx/services/xxxs";
  Service service = new Service();
  try {
  Call call = (Call) service.createCall();
  call.setTimeout(new Integer(60000));
  call.setTargetEndpointAddress(new URL(endpoint));
  call.setOperationName(new QName(soapaction, "getXxxx"));
  SOAPHeaderElement headerElement = new SOAPHeaderElement(soapaction, "AuthHeaderCS");
  headerElement.setNamespaceURI(soapaction);
  headerElement.addChildElement("username").setValue("xxxx");
  headerElement.addChildElement("application").setValue("Xxxx");
  headerElement.addChildElement("department").setValue("TestDept");
  call.addHeader(headerElement);
  call.setReturnType(XMLType.XSD_STRING);
  call.addParameter("o", XMLType.XSD_STRING, ParameterMode.IN);
  String ret = (String) call.invoke(new Object[] { null });
  System.out.println(ret); 
  } catch (Exception e) {
  e.printStackTrace();
  }
 }
}

在傳值時需要注意的是不是以前的

call.addParameter("o", XMLType.XSD_STRING, ParameterMode.IN);

需要將參數存到SOAPHeaderElement中,然後再call.addHeader,就可以了。

SOAPHeaderElement headerElement = new SOAPHeaderElement(soapaction, "AuthHeaderCS");
  headerElement.setNamespaceURI(soapaction);
  headerElement.addChildElement("username").setValue("xxxx");
  headerElement.addChildElement("application").setValue("Xxxx");
  headerElement.addChildElement("department").setValue("TestDept");
  call.addHeader(headerElement);

Copyright © Linux教程網 All Rights Reserved