参考:
java调用webservice接口的几种方法
2021/6/25 SOAP学习-WebService全套实战教程
1、使用wsdl2java把WSDL文件转成本地类
参考 例子
2021/6/25 SOAP学习-WebService全套实战教程
public static void way1(){
/**
* 1、wsdl网址
* http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx?wsdl
*
* 2、在需要放反编译代码的位置输入命令
* wsimport -s . http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx?wsdl
*
* 3、找标签 service portType operation
* <wsdl:service name="MobileCodeWS"></wsdl:service> 找到服务类 MobileCodeWS
* <wsdl:portType name="MobileCodeWSSoap"> 找到接口实现类 MobileCodeWSSoap
* <wsdl:operation name="getDatabaseInfo"> 找到具体方法 getDatabaseInfo
*
* 4、开始以下代码
*/
try {
cn.com.webxml.MobileCodeWS mobileCodeWS = new cn.com.webxml.MobileCodeWS();
cn.com.webxml.MobileCodeWSSoap port=mobileCodeWS.getPort(cn.com.webxml.MobileCodeWSSoap.class);
String query=port.getMobileCodeInfo("18888888888",null);
System.out.println(query);
} catch (Exception e) {
e.printStackTrace();
}
}
2、直接AXIS调用远程的web service
例子的网址是不行的,换一个可能就行了
try {
/**
* http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx?wsdl
* MobileCodeWS
* MobileCodeWSSoap
* getMobileCodeInfo
* 18888888888
*/
String endpoint = "http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx?wsdl";//?wsdl
//直接引用远程的wsdl文件
//以下都是套路
Service service = new Service();
Call call = (Call) service.createCall();
call.setTargetEndpointAddress(new URL(endpoint));
call.setOperationName(new QName("getMobileCodeInfo"));//WSDL里面描述的方法名称
// call.addParameter("getMobileCodeInfo", org.apache.axis.encoding.XMLType.XSD_DATE,
// javax.xml.rpc.ParameterMode.IN);//接口的参数
// call.setReturnType(org.apache.axis.encoding.XMLType.XSD_STRING);//设置返回类型
call.addParameter( "mobileCode", org.apache.axis.encoding.XMLType.XSD_STRING, ParameterMode.IN );//调用方法参数1
call.addParameter( "userID", org.apache.axis.encoding.XMLType.XSD_STRING, ParameterMode.IN );//调用方法参数2
call.setReturnType(org.apache.axis.encoding.XMLType.XSD_STRING);//设置返回类型
call.setSOAPActionURI("http://WebXml.com.cn/"+"getMobileCodeInfo");//命名空间+调用方法名
String temp = "18888888888";
String result = (String) call.invoke(new Object[]{temp,""});//给方法传递参数,并且调用方法
System.out.println("result is " + result);
} catch (Exception e) {
System.err.println(e.toString());
}
3、直接SOAP调用远程的webservice
参考
java调用webservice接口的几种方法
我没试成功,先不深究了
import org.apache.soap.util.xml.*;
import org.apache.soap.*;
import org.apache.soap.rpc.*;
import java.io.*;
import java.net.*;
import java.util.Vector;
public class caService {
public static String getService(String user) {
URL url = null;
try {
url = new URL("http://192.168.0.100:8080/ca3/services/caSynrochnized");
} catch (MalformedURLException mue) {
return mue.getMessage();
}
// This is the main SOAP object
Call soapCall = new Call();
// Use SOAP encoding
soapCall.setEncodingStyleURI(Constants.NS_URI_SOAP_ENC);
// This is the remote object we're asking for the price
soapCall.setTargetObjectURI("urn:xmethods-caSynrochnized");
// This is the name of the method on the above object
soapCall.setMethodName("getUser");
// We need to send the ISBN number as an input parameter to the method
Vector soapParams = new Vector();
// name, type, value, encoding style
Parameter isbnParam = new Parameter("userName", String.class, user, null);
soapParams.addElement(isbnParam);
soapCall.setParams(soapParams);
try {
// Invoke the remote method on the object
Response soapResponse = soapCall.invoke(url, "");
// Check to see if there is an error, return "N/A"
if (soapResponse.generatedFault()) {
Fault fault = soapResponse.getFault();
String f = fault.getFaultString();
return f;
} else {
// read result
Parameter soapResult = soapResponse.getReturnValue();
// get a string from the result
return soapResult.getValue().toString();
}
} catch (SOAPException se) {
return se.getMessage();
}
}
}
4、用http调用
xxx
5、异常
5.1、webService 报aultString: org.xml.sax.SAXException: Bad envelope tag: definitions
参考:https://blog.csdn.net/wangjinwei6912/article/details/8512598