功能要求:需要在ERP中调用其他web服务或者自身web服务(比如跨账套过账等)
1.编写java程序,并将程序打包成jar包
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; public class KbdErpHttpClient { public static String doPost(String soapXML){ //创建HttpClientBuilder HttpClientBuilder httpClientBuilder = HttpClientBuilder.create(); //创建HTTPClient CloseableHttpClient closeableHttpClient = httpClientBuilder.build(); //创建HTTPPOST HttpPost httpPost = new HttpPost("http://xxx.xxx.xxx.xxx/web/ws/r/aws_ttsrv2"); //设置超时时间 RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(1000*30).setConnectTimeout(1000*30).build(); httpPost.setConfig(requestConfig); //System.out.println("配置完成"); //创建返回响应体 String resp = null; try{ //配置post请求头 httpPost.setHeader("Content-Type","text/xml;charset=UTF-8"); httpPost.setHeader("SOAPAction","\"\""); httpPost.setHeader("Connection","keep-alive"); httpPost.setHeader("Accept-Encoding","gzip,deflate"); //配置post请求体 StringEntity data = new StringEntity(soapXML,"UTF-8"); data.setContentType("application/xml"); httpPost.setEntity(data); //发送请求 CloseableHttpResponse response = closeableHttpClient.execute(httpPost); //获取响应体 HttpEntity httpEntity = response.getEntity(); //System.out.println(response.getStatusLine().getStatusCode()); if(httpEntity != null){ resp = EntityUtils.toString(httpEntity,"UTF-8"); } closeableHttpClient.close(); }catch (Exception e){ System.out.println(e.toString()); } return resp; } }
打包jar
上传JAR到服务器,并将jar包添加至classpath中
2.在程序中引用上一部的jar包,并调用相关类的静态方法
FUNCTION i001_http_test() DEFINE l_str STRING DEFINE l_xml_str STRING LET l_xml_str = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" ", "xmlns:tip=\"http://www.dsc.com.tw/tiptop/TIPTOPServiceGateWay\">\n", " <soapenv:Header/>\n", " <soapenv:Body>\n", " <tip:OperaNorPostRequest>\n", " <tip:request><![CDATA[<Request>\n", " <Access>\n", " <Authentication user='tiptop' password=''/>\n", " <Organization name='WCTZ'/>\n", " </Access>\n", " <RequestContent>\n", " <Parameter>\n", " <Record>\n", " <Field name='type' value='DB'/>\n", " <Field name='rec' value='K141-221207001'/>\n", " </Record>\n", " </Parameter>\n", " </RequestContent>\n", "</Request>]]></tip:request>\n", " </tip:OperaNorPostRequest>\n", " </soapenv:Body>\n", "</soapenv:Envelope>" LET l_str = KbdErpHttpClient.doPost(l_xml_str) DISPLAY l_str {DEFINE req com.HTTPRequest IF resp.getStatusCode() != 200 THEN DISPLAY "HTTP Error ("||resp.getStatusCode()||") ",resp.getStatusDescription() ELSE DISPLAY "HTTP Response is : ",resp.getTextResponse() END IF CATCH DISPLAY "ERROR :",STATUS||" ("||SQLCA.SQLERRM||")" END TRY} END FUNCTION
标签:ERP,自定义,org,jar,httpPost,import,apache,http,resp From: https://www.cnblogs.com/smarttony/p/16968441.html