zoukankan      html  css  js  c++  java
  • 接口工具类

    该工具类主要用于调用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("&lt;", "<").replaceAll("&gt;", ">");
                    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/> " +
                        " <soapenv:Body> " +
                        " <tip:methodName> " +
                        " <tip:request> " +
                        "mouldCode " +
                        " </tip:request> " +
                        " </tip:methodName> " +
                        " </soapenv:Body> " +
                        " </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 = " &lt;Request>" +
                    "&lt;Access>" +
                    "&lt;Authentication user="tiptop" password="tiptop"/>" +
                    "&lt;Connection application="EFGP" source="192.168.1.2"/>" +
                    "&lt;Organization name='B-SLB'/>" +
                    "&lt;Locale language="zh_tw"/>" +
                    "&lt;/Access>&lt;RequestContent>     " +
                    "&lt;Document>&lt;RecordSet id="1">" +
                    "&lt;Master name="apa_file">" +
                    "&lt;Record>" +
                    "&lt;Field name="apa01" value='APE-B19000001'/>" +
                    "&lt;Field name="apa02" value='2019-01-13'/> " +
                    "&lt;Field name="apa00" value='付款'/>" +
                    "&lt;Field name="apaud02" value='CS190423200736256B'/> " +
                    "&lt;Field name="apa41" value="Y"/>" +
                    " &lt;/Record>" +
                    "&lt;/Master>" +
                    "&lt;/RecordSet>" +
                    "&lt;/Document>" +
                    "&lt;/RequestContent>" +
                    "&lt;/Request>";

            String d=queryPmMessage(webserviceurl, "CreateAapt150_yRequest", ss);
            System.out.println("dd:"+d);
            
         
        }
    }

  • 相关阅读:
    Spring 整合 Redis
    Spring 整合 Redis
    C#,Java,MD5加密对等实现
    Java调用JavaWebService
    c#,Java aes加密
    Kettle环境变量配置
    Maven环境配置
    Win10,JDK8,tomact7.0.85配置
    ReverseEngineerCodeFirst 自定义模板
    Aspnet_Session
  • 原文地址:https://www.cnblogs.com/cjxns/p/11125718.html
Copyright © 2011-2022 走看看