Interface Tools

The principal tool used to call the 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;


HttpClientSoapUtil class {public
    static int socketTimeout = 30000; // request time
    static int connectTimeout = 30000; // wait timeout
    static Logger Logger = Logger.getLogger (HttpClientSoapUtil.class);

 
    / **
     * the ERP Interface tools
     *
     * @ param postUrl address
     * @param soapXml xml splicing
     * soapAction-coded text / XML; charset = UTF-. 8
     * @return
     * /
    public static String doPostSoap1_3 (posturl String, String soapXml) {        
        String retstr = "";
        String DATAS = "" ;
        // create 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 ( "post-processing results:" + DATAS);
                // Jiexi (DATAS); // do not resolve temporarily, the result set is returned to the calling class
                return DATAS;
            }
            // release resources
            closeableHttpClient.close ();
        the catch} (Exception E) {
            logger.error ( "Exception in doPostSoap1_3", E);
            return DATAS;
        }
        return DATAS;
        
    }

    / **
     * xml interface to the ERP processing tools
     *
     * @param URL address
     * @param methodName method name
     @param mouldCode XML *
     * /
    public static String queryPmMessage (URL String, 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 ( "form not successfully transmitted to the ERP!");
            } the else {
                 System.out.println ( "form successfully sent to the ERP!");
            }
           
          } the catch (Exception E) {
              e.printStackTrace ();
          }
    }
    / **
     * attributes may be obtained by a recursive method,
     * @param Element
     * /
    public static void Analysis (the Element Element) {
        List <the Element> Children element.elements = ();
        ! IF (Children = null && children.isEmpty! ()) {
            for (the Element Child:Children) {

                System.out.println ( "acquired data:" + 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);
        
     
    }
}

Guess you like

Origin www.cnblogs.com/cjxns/p/11125718.html