Infor XA ERP二次開發,目前最好的最方便的交互就是通過發送SystemLink請求
這里記錄個人在實際工作中java編寫的發送SystemLink請求工具
package cn.markwins.yinfor.utils.net;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.net.HttpURLConnection;import java.net.URL;import java.net.URLDecoder;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import java.util.Set;import org.apache.log4j.Logger;import org.apache.http.HttpEntity;import org.apache.http.HttPResponse;import org.apache.http.NameValuePair;import org.apache.http.client.ClientProtocolException;import org.apache.http.client.entity.UrlEncodedFormEntity;import org.apache.http.client.methods.HttpPost;import org.apache.http.entity.BufferedHttpEntity;import org.apache.http.impl.client.CloseableHttpClient;import org.apache.http.impl.client.HttpClientBuilder;import org.apache.http.message.BasicNameValuePair;import cn.markwins.yinfor.global.GlobalParameters;import cn.markwins.yinfor.utils.common.StringTools;/** * @Description Http網絡請求工具欄 * @author 李yi輝 * @date 2016年3月23日 */public class HttpTools { private static final String encodingUTF8 ="UTF-8"; private static final Logger logger = Logger.getLogger(HttpTools.class); /** * @Description 發送Http請求 * @param url 請求地址 * @param xml xml請求內容 * @return String 請求后的響應消息 */ public static Map<Boolean,String> postXMLRequest(String url, String xml){ if(StringTools.isNullOrWhiteSpace(xml) || StringTools.isNullOrWhiteSpace(xml)){ return null; } HttpURLConnection httpConn = null; OutputStream os = null; InputStream is = null; StringBuffer responseBuffer = null; Boolean postStatus = false; String postMsg = ":發送SystemLink請求失敗"; try { //1、得到http連接 httpConn = (HttpURLConnection) new URL(url).openConnection(); //2、設置http請求參數 httpConn.setRequestMethod("POST"); httpConn.setDoInput(true); httpConn.setDoOutput(true); httpConn.setUseCaches(false); httpConn.setConnectTimeout(50000); httpConn.setReadTimeout(50000); httpConn.setRequestProperty("Content-Type", "text/xml; charset=UTF-8"); //3、通過http連接服務器 httpConn.connect(); //4、向服務器發送xml數據 os = httpConn.getOutputStream(); os.write(xml.getBytes()); os.flush(); //5、得到http請求后,服務器返回的響應 int responseCode = httpConn.getResponseCode(); switch (responseCode) { case 200: is = httpConn.getInputStream(); int length = 0; byte[] buffer = new byte[1024]; responseBuffer = new StringBuffer(); while((length=is.read(buffer)) != -1){ responseBuffer.append(new String(buffer,0,length,GlobalParameters.ENCODING)); } if(responseBuffer.length() > 10){ postStatus = true; postMsg = URLDecoder.decode(responseBuffer.toString().trim(), "UTF-8"); }else{ postMsg = "200:" + responseBuffer.toString(); } break; case 400: postMsg = "400:錯誤請求"; break; case 404: postMsg = "404:未找到"; break; case 408: postMsg = "408:請求超時"; break; case 500: postMsg = "500:SystemLink XA服務器錯誤"; break; default: postMsg = responseCode + postMsg; break; } //6、返回響應消息 Map<Boolean, String> resultMap = new HashMap<Boolean, String>(); resultMap.put(postStatus, postMsg); return resultMap; } catch (Exception e) { logger.error("發送Http請求失敗", e); } finally { try { if(is != null){ is.close(); is = null; } if(os != null){ os.close(); os = null; } if(httpConn != null){ httpConn.disconnect(); httpConn = null; } } catch (IOException e) { logger.error("關閉Http請求連接資源失敗", e); } } return null; }}我們將SystemLink請求的響應結果放在了一個map里面,通過map的key值ture/false就可以知道請求是否發送成功,
如果成功了,就可以解析出SystemLink的響應報文,我們在下一篇里面介紹如何解析SystemLink的報文信息。
http://blog.csdn.net/yihuiworld
新聞熱點
疑難解答