使用ksoap2调用远程webservice,代码如下:
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
//...
String WSDL_URI = "http://ncp.szscgl.com:9000/Interface/sDataInfrace.asmx?WSDL";
String NAMESPACE = "http://tempuri.org/";//namespace
String methodName = "checkIn"; //要调用的方法名称
String paramXml = "..."; //发送的内容,例如一段xml
String soapAction = NAMESPACE + methodName;
SoapObject request = new SoapObject(NAMESPACE, methodName);
request.addProperty("strXML", paramXml); //参数名称是strXML
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapSerializationEnvelope.VER11);
envelope.bodyOut = request; //由于是发送请求,所以是设置bodyOut
envelope.dotNet = true; //由于是.net开发的webservice,所以这里要设置为true
try{
HttpTransportSE httpTransportSE = new HttpTransportSE(WSDL_URI);
httpTransportSE.call(soapAction, envelope);
SoapObject object = (SoapObject) envelope.bodyIn;
return object.getProperty(0).toString(); //返回的结果
}catch(Exception e){
e.printStackTrace();
System.out.println(methodName + "报错:"+ e.getMessage());
return null;
}
jar的引用:
相比axis,ksoap2更好用。