第一中方法 cxf 调用这个需要依赖于 一个cxf.2.3.3.jar包 这个是下载地址 链接: https://pan.baidu.com/s/1nvQanXJ 密码: 79n6 但这中办法相对比较慢 测试如果保存的话如果3秒左右 但这种方法相遇对比较简单
package com.siit.image.test;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
public class Test {
public static Object[] invokeRemoteMethod(String url, String method, Object[] parameters) {
JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
if (!url.endsWith("wsdl")) {
url += "?wsdl";
}
org.apache.cxf.endpoint.Client client = dcf.createClient(url);
try {
Object[] objects = client.invoke(method, parameters);
return objects;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static void main(String[] args) {
String url = "这个就是提供的地址";
Object[] parameters = {"传的拼接XML的参数"};
Object[] updateInvoices = invokeRemoteMethod(url, "方法名", parameters);
System.out.println((String)updateInvoices[0]);
}
}
需要jar包: https://pan.baidu.com/s/1mihwWEc 密码: ha6r 密码: ayw6 速度较快 差不多1秒左右肯定能响应完成
package com.siit.image.util;
public class HttpClientUtil {
private static Logger log = Logger.getLogger(HttpClientUtil.class);
public static String HttpPost(String url,String xml){
String jsonparam = "";
CloseableHttpClient httpClient = null;
try {
httpClient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url); //webservice服务地址
String soapRequestData = getRequestXml(xml); //soap协议的格式,定义了方法和参数
HttpEntity re = new StringEntity(soapRequestData,HTTP.UTF_8);
httppost.setHeader("Content-Type","application/soap+xml; charset=utf-8");
httppost.setEntity(re);
HttpResponse response = httpClient.execute(httppost); //调用接口
//输出的xml
log.info("httppost.getEntity() == EntityUtils.toString: " + EntityUtils.toString(httppost.getEntity()));
//返回是否成功的状态
System.out.println(response.getStatusLine().getStatusCode());
if(response.getStatusLine().getStatusCode() == 200) {
//获得输出的字符串 `
String xmlString = EntityUtils.toString(response.getEntity());
jsonparam = parseXMLSTRING(xmlString);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
httpClient.getConnectionManager().shutdown(); //关闭连接
}
return jsonparam;
}
/**
* 获得请求XML
* @return
*/
private static String getRequestXml(String xml){ //这个请求的地址如果去提供的服务地址加上?wsdl去取
StringBuilder sb = new StringBuilder();
sb.append("<?xml version=\"1.0\"?>");
sb.append("<soap:Envelope xmlns:soap=\"http://www.w3.org/2003/05/soap-envelope\" xmlns:tem=\"http://tempuri.org/\">");
sb.append("<soap:Header/>");
sb.append("<soap:Body>");
sb.append("<tem:UpdateInvoice>");
sb.append("<tem:inxml><![CDATA["+xml+"]]></tem:inxml></tem:UpdateInvoice>");
sb.append("</soap:Body>");
sb.append("</soap:Envelope>");
return sb.toString();
}
//解析xml文件,获取其中的返回值
public static String parseXMLSTRING(String xmlString){
String returnJson = "";
try {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = (Document) builder.parse(new InputSource(new StringReader(xmlString)));
Element root = doc.getDocumentElement();//根节点
Node node = root.getFirstChild();
while(!node.getNodeName().equals("UpdateInvoiceResult")) {
node = node.getFirstChild();
}
String paramJosn = "";
if(node.getFirstChild() != null) paramJosn = node.getFirstChild().getNodeValue();
XMLSerializer xmlSerializer=new XMLSerializer();
JSONObject json=(JSONObject) xmlSerializer.read(paramJosn.toString());//把xml转成json的格式
log.error("接口出参:"+json);
} catch (Exception e) {
e.printStackTrace();
}
return returnJson;
}
public static void main(String[] args) {
String url = "服务地址";
String xml = "拼接的xml";
String returns = HttpClientUtil.HttpPost(url, xml);
System.out.println(returns);
}
}
第三种方式 axis方式调用 https://pan.baidu.com/s/1slVrGxn 密码: 8mv9 这个效率也可以
import java.rmi.RemoteException;
import javax.xml.rpc.ServiceException;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
public class Test {
public static void main(String[] args) throws ServiceException, RemoteException {
String endpoint = "提供服务的地址";
Service service = new Service();
Call call = (Call) service.createCall();
call.setTargetEndpointAddress(endpoint);
call.setOperationName("提供的地址的方法");
call.setUseSOAPAction(true);
//这个在提供地址的下面直接搜索SOAPAction 就能找个这个地址
call.setSOAPActionURI("http://tempuri.org/HelloWorld");
call.addParameter("s", org.apache.axis.encoding.XMLType.XSD_STRING,
javax.xml.rpc.ParameterMode.IN);//入参类型和出参类型
call.setReturnType(org.apache.axis.encoding.XMLType.XSD_STRING);//设置返回类型
String result = (String)call.invoke(new Object[]{"传入的参数"});
//给方法传递参数,并且调用方法
System.out.println("result is "+result);
}
}