webservice有提供wsdl跟asmx两种方式,两种方式的调用方法不同
1、wsdl的方式
添加依赖:
org.apache.cxf
cxf-rt-frontend-jaxws
3.2.9
org.apache.cxf
cxf-rt-transports-http
3.2.9
请求代码:
JaxWsDynamicClientFactory clientFactory = JaxWsDynamicClientFactory.newInstance();
Client client = clientFactory.createClient(webserviceAddress);
Object[] result = null;
try {
//如果有命名空间的话
QName operationName = new QName(namespaceURI,localPort); //如果有命名空间需要加上这个,第一个参数为命名空间名称,第二个参数为WebService方法名称
result = client.invoke(operationName,param1, param2);//后面为WebService请求参数数组
//如果没有命名空间的话
result = client.invoke(operationName, param1); //注意第一个参数是字符串类型,表示WebService方法名称,第二个参数为请求参数
} catch (Exception e) {
String errMsg = "WebService发生异常!";
result = new Object[] { errMsg };
logger.error(errMsg, e);
}
参考地址:https://blog.csdn.net/lichuangcsdn/article/details/83053327
2、asmx的方式
maven依赖
org.apache.axis
axis
1.4
javax.xml.rpc
javax.xml.rpc-api
1.1.2
commons-discovery
commons-discovery
0.5
Java代码:
import javax.xml.namespace.QName;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
/**调用webservice.asmx接口
*
* @param method 需要调用的方法
* @param json 传递的参数
*/
public static void callWebserviceASMX(String method,String json){
//获取webservice接口地址
String url = "http://xxxx/xx/Webservice.asmx";
//获取域名地址,server定义的
String soapaction = "http://tempuri.org/";
Service service = new Service();
try {
Call call = (Call) service.createCall();
call.setTargetEndpointAddress(url);
//设置要调用的方法
call.setOperationName(new QName(soapaction,method));
//设置要返回的数据类型
call.setReturnType(new QName(soapaction,method), String.class);
call.setUseSOAPAction(true);
call.setSOAPActionURI(soapaction+method);
//调用方法并传递参数
String result = (String) call.invoke(new Object[]{json});
System.out.println("result is:::"+result);
} catch (ServiceException e) {
e.printStackTrace();
} catch (RemoteException e) {
e.printStackTrace();
}
}
You must call addParameter() for all parameters if you have called setReturnType():
----
// 设置参数名称,具体参照从浏览器中看到的
call.addParameter(new QName(soapaction, method),
XMLType.XSD_STRING, ParameterMode.IN); //设置请求参数及类型
faultCode: {http://schemas.xmlsoap.org/soap/envelope/}Server.userException
faultSubcode:
faultString: org.xml.sax.SAXException: SimpleDeserializer encountered a child element, which is NOT expected, in something it was trying to deserialize.
----设置parameter处的参数名不对
// 设置参数名称,具体参照从浏览器中看到的
call.addParameter(new QName(soapaction, "model"),
XMLType.XSD_STRING, ParameterMode.IN); //设置请求参数及类型
自己调试后的代码:
//获取webservice接口地址
String url = "http://101.1.1.1:8999/DataPlatform/66KC/Transfer.asmx";
//获取域名地址,server定义的
String soapaction = "http://tempuri.org/";
String method = "ReceiveDelivey" ;
Service service = new Service();
ShengaoPushTransportInfo info = new ShengaoPushTransportInfo();
info.setEntrustId(6572678081465549845L);
String request = gson.toJson(info);
try {
Call call = (Call) service.createCall();
call.setTargetEndpointAddress(new java.net.URL(url));
call.setUseSOAPAction(true);
call.setSOAPActionURI(soapaction+method);
//设置要调用的方法
call.setOperationName(new QName(soapaction,method));
// 设置参数名称,具体参照从浏览器中看到的
call.addParameter(new QName(soapaction, "request"),
XMLType.XSD_STRING, ParameterMode.IN); //设置请求参数及类型
//设置要返回的数据类型
call.setReturnType(new QName(soapaction,method), String.class);
//调用方法并传递参数
String result = (String) call.invoke(new QName(soapaction,method),new Object[]{request});
System.out.println("result is:::"+result);
}catch (ServiceException se){
se.printStackTrace();
}catch (RemoteException re) {
re.printStackTrace();
}catch (Exception e){
e.printStackTrace();
}
WebService几个常见的异常 :https://blog.csdn.net/chenjunan888/article/details/8753184
参考地址:https://www.cnblogs.com/zhoulian/p/9323908.html
3、httpOK,http模式调用
public static void main(String args[]){
OkHttpClient client = new OkHttpClient();
MediaType mediaType = MediaType.parse("text/xml;charset=utf-8");
RequestBody body = RequestBody.create(mediaType,
"\n\n \n \n \n 6572678081465549845 \n WTD20190829000012 \n EMXJD2019082900001 \n 6572678592684098581 \n YSD20190829000009 \n 1112233 \n 15.000 \n 2019-08-29 11:19:40 \n Y003 \n \n \n \n ");
Request request = new Request.Builder()
.url("http://101.1.1.1:8999/DataPlatform/66KC/Transfer.asmx")
.method("POST", body)
.addHeader("Content-Type", "text/xml;charset=utf-8")
.build();
try {
Response response = client.newCall(request).execute();
System.out.println(response);
System.out.println(response.body().string());
} catch (IOException e) {
e.printStackTrace();
}
}
}
麻烦点是拼接参数麻烦