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