接口工具类
Posted cjxns
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了接口工具类相关的知识,希望对你有一定的参考价值。
该工具类主要用于调用ERP
import java.nio.charset.Charset;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.apache.log4j.Logger;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
public class HttpClientSoapUtil
static int socketTimeout = 30000;// 请求超时时间
static int connectTimeout = 30000;// 传输超时时间
static Logger logger = Logger.getLogger(HttpClientSoapUtil.class);
/**
* ERP接口工具类
*
* @param postUrl 地址
* @param soapXml xml拼接
* soapAction 写死 text/xml;charset=UTF-8
* @return
*/
public static String doPostSoap1_3(String postUrl, String soapXml)
String retStr = "";
String datas="";
// 创建HttpClientBuilder
HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
// HttpClient
CloseableHttpClient closeableHttpClient = httpClientBuilder.build();
HttpPost httpPost = new HttpPost(postUrl);
// 设置请求和传输超时时间
RequestConfig requestConfig = RequestConfig.custom()
.setSocketTimeout(socketTimeout)
.setConnectTimeout(connectTimeout).build();
httpPost.setConfig(requestConfig);
try
httpPost.setHeader("Content-Type", "text/xml;charset=UTF-8");
// 调用ERP
httpPost.setHeader("soapAction", "text/xml;charset=UTF-8");
StringEntity data = new StringEntity(soapXml,Charset.forName("UTF-8"));
httpPost.setEntity(data);
CloseableHttpResponse response = closeableHttpClient
.execute(httpPost);
HttpEntity httpEntity = response.getEntity();
if (httpEntity != null)
// 打印响应内容
System.out.println("调用成功!!!");
retStr = EntityUtils.toString(httpEntity, "UTF-8");
datas=retStr.replaceAll("<", "<").replaceAll(">", ">");
System.out.println("成功后返回的结果:"+datas);
if(datas.indexOf("<Response>")!=-1 && datas.indexOf("</fjs1:response>")!=-1 )
datas=datas.substring(datas.indexOf("<Response>"), datas.indexOf("</fjs1:response>"));
System.out.println("处理后结果:"+datas);
//jiexi(datas); // 暂时不用解析,把结果集返回到调用类
return datas;
// 释放资源
closeableHttpClient.close();
catch (Exception e)
logger.error("exception in doPostSoap1_3", e);
return datas;
return datas;
/**
* ERP接口xml处理工具类
*
* @param url 地址
* @param methodName 方法名
* @param mouldCode xml
*/
public static String queryPmMessage(String url, String methodName, String mouldCode)
String xml = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:tip=\"接口地址写死的\">"+
" <soapenv:Header/>\n" +
" <soapenv:Body>\n" +
" <tip:methodName>\n" +
" <tip:request>\n" +
"mouldCode\n" +
" </tip:request>\n" +
" </tip:methodName>\n" +
" </soapenv:Body>\n" +
" </soapenv:Envelope>";
xml = xml.replaceAll("methodName", methodName).replaceAll("mouldCode", mouldCode);
return HttpClientSoapUtil.doPostSoap1_3(url, xml);
public static void jiexi(String str)
try
Document doc = DocumentHelper.parseText(str);
Element root = doc.getRootElement();// 获取根节点
Element bodyElement = root.element("Execution");
Element rankElement = bodyElement.element("Status");
String code = rankElement.attributeValue("code");
String sqlcode = rankElement.attributeValue("sqlcode");
String description = rankElement.attributeValue("description");
System.out.println("获得属性code值:" + code+"sqlcode值:" + sqlcode+"description值:" + description);
if (((code != "0") && (!code.equals("0"))) || ((sqlcode != "0") && (!sqlcode.equals("0"))))
System.out.println("表单没有成功发送到erp!");
else
System.out.println("表单成功发送到erp!");
catch(Exception e)
e.printStackTrace();
/**
* 可以用递归方法获取属性,
* @param element
*/
public static void analysis(Element element)
List<Element> children=element.elements();
if(null!=children&&!children.isEmpty())
for (Element child:children)
System.out.println("获取到的数据:"+child.toString());
analysis(child);
else
List<Attribute> attributes=element.attributes();
if(null!=attributes&&!attributes.isEmpty())
for (Attribute attribute:attributes)
System.out.println("获取到的数据:"+attribute.getName()+"="+attribute.getValue());
public static void main(String[] args)
//String webserviceurl = "http://10.200.2.10:6384/ws/r/aws_ttsrv2_toptest?wsdl";
String plant="G-SLB";
String oano="cgd20190110001";
String ss = " <Request>" +
"<Access>" +
"<Authentication user=\"tiptop\" password=\"tiptop\"/>" +
"<Connection application=\"EFGP\" source=\"192.168.1.2\"/>" +
"<Organization name=‘B-SLB‘/>" +
"<Locale language=\"zh_tw\"/>" +
"</Access><RequestContent> " +
"<Document><RecordSet id=\"1\">" +
"<Master name=\"apa_file\">" +
"<Record>" +
"<Field name=\"apa01\" value=‘APE-B19000001‘/>" +
"<Field name=\"apa02\" value=‘2019-01-13‘/> " +
"<Field name=\"apa00\" value=‘付款‘/>" +
"<Field name=\"apaud02\" value=‘CS190423200736256B‘/> " +
"<Field name=\"apa41\" value=\"Y\"/>" +
" </Record>" +
"</Master>" +
"</RecordSet>" +
"</Document>" +
"</RequestContent>" +
"</Request>";
String d=queryPmMessage(webserviceurl, "CreateAapt150_yRequest", ss);
System.out.println("dd:"+d);
以上是关于接口工具类的主要内容,如果未能解决你的问题,请参考以下文章
Java源码分析集合框架-Collections工具类-Arrays工具类
Java学习笔记6.3.4 文件操作 - Path接口和Files工具类