1. 程式人生 > 其它 >自定義jar包供ERP使用

自定義jar包供ERP使用

功能要求:需要在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