调用对方系统的http接口,post请求。
RequestParamsVo requestParamsVo =new RequestParamsVo();
groupRequestParamsVo(requestParamsVo, token, String.valueOf(maxKuduId));
//将requestParamsVo 转为json字符串
String jsonstr = JSONArray.toJSON(requestParamsVo).toString();
//主要是用到了BigdataHttpUtil这个工具类
byte[] orderBuf = BigdataHttpUtil.doPostJson(dataUrl,jsonstr);
ps:工具类 BigdataHttpUtil.java(HttpUtil)
package com.sdp.common;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.ByteArrayRequestEntity;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.RequestEntity;
import org.apache.commons.httpclient.methods.StringRequestEntity;
import com.global.exception.baseException;
import com.global.util.StringUtil;
public class BigdataHttpUtil {
public static byte[] doPost(String url) {
Map
return doPost(url, map);
}
public static byte[] doPost(String url, Map
return doPost(url, map, "UTF-8");
}
public static byte[] doPost(String url, Map
HttpClient client = new HttpClient();//22
client.getParams().setSoTimeout(30000);
PostMethod method = new PostMethod(url);
// 设置字符编码集
method.getParams().setContentCharset(enCode);
for (String key : map.keySet()) {
String value = map.get(key);
if (StringUtil.isBlank(value))
continue;
method.addParameter(key, value);
}
try {
int status = client.executeMethod(method);
if (status != HttpStatus.SC_OK)
throw new Exception(String.format("服务器返回错误:[%d]", status));
return method.getResponseBody();
} catch (Exception ex) {
throw new baseException(String.format("发送数据失败:[%s]", ex.getMessage()), ex);
} finally {
method.releaseConnection();
}
}
public static byte[] doPost(String url, byte[] buf) {
return doPost(url, buf, "UTF-8");
}
public static byte[] doPost(String url, byte[] buf, String enCode) {
HttpClient client = new HttpClient();
client.getParams().setSoTimeout(60000);
PostMethod method = new PostMethod(url);
method.setRequestHeader("Content-Type", "application/xml; charset=" + enCode);
method.setRequestEntity(new ByteArrayRequestEntity(buf));
try {
int status = client.executeMethod(method);
if (status != HttpStatus.SC_OK)
throw new Exception(String.format("服务器返回错误:[%d]", status));
return method.getResponseBody();
} catch (Exception ex) {
throw new baseException(String.format("发送数据失败:[%s]", ex.getMessage()), ex);
} finally {
method.releaseConnection();
}
}
public static byte[] doPostJson(String url, String jsonstr) {
return doPostJson(url, jsonstr, "");
}
public static byte[] doPostJson(String url, String jsonstr, String sign) {
HttpClient client = new HttpClient();
client.getParams().setSoTimeout(60000);
PostMethod method = new PostMethod(url);
method.setRequestHeader("Content-Type", "application/json; charset=UTF-8");
if (StringUtil.isNotBlank(sign)) {
method.setRequestHeader("sign", sign); // 签名
}
RequestEntity se;
try {
se = new StringRequestEntity (jsonstr ,"application/json" ,"UTF-8");
method.setRequestEntity(se);
} catch (UnsupportedEncodingException e) {
throw new baseException(String.format("发送数据错误:[%s][%s]", e.getMessage(), url), e);
}
try {
int status = client.executeMethod(method);
if (status != HttpStatus.SC_OK)
throw new Exception(String.format("服务器返回错误:[%d]", status));
return method.getResponseBody();
} catch (Exception ex) {
throw new baseException(String.format("发送数据失败:[%s]", ex.getMessage()), ex);
} finally {
method.releaseConnection();
}
}
public static byte[] doGet(String url) {
return doGet(url, "UTF-8");
}
public static byte[] doGet(String url, String enCode) {
Map
return doGet(url, map, enCode);
}
public static byte[] doGet(String url, Map
HttpClient client = new HttpClient();
client.getParams().setSoTimeout(60000);
String Params = "";
String flag = "";
if (map != null && !map.isEmpty()) {
flag = "&";
if (url.indexOf('?') < 0) {
flag = "?";
}
for (String key : map.keySet()) {
String value = map.get(key);
if (StringUtil.isBlank(value))
continue;
if (StringUtil.isBlank(Params)) {
Params = flag+key + "=" + value;
} else {
Params += "&"+key + "=" + value;
}
}
}
GetMethod method = new GetMethod(url + Params);
// 设置字符编码集
method.getParams().setContentCharset(enCode);
try {
int status = client.executeMethod(method);
if (status != HttpStatus.SC_OK)
throw new Exception(String.format("服务器返回错误:[%d]", status));
return method.getResponseBody();
} catch (Exception ex) {
throw new baseException(String.format("发送数据失败:[%s]", ex.getMessage()), ex);
} finally {
method.releaseConnection();
}
}
public static String getParams(String method,Map
{
String params="";
Set
String beginLetter="";
if (method.equalsIgnoreCase("get"))
{
beginLetter="?";
}
for (Iterator
String s = (String) it.next();
if (params.equals(""))
{
params += beginLetter + s + "=" + paramValues.get(s);
}
else
{
params += "&" + s + "=" + paramValues.get(s);
}
}
return params;
}
public static String getParams(String method,Map
{
String params="";
Set
String beginLetter="";
if (method.equalsIgnoreCase("get"))
{
beginLetter="?";
}
for (Iterator
String s = (String) it.next();
String value = paramValues.get(s);
if ("1".equals(encode)) {
value = URLEncoder.encode(paramValues.get(s), "UTF-8");
}
if (params.equals(""))
{
params += beginLetter + s + "=" + value;
}
else
{
params += "&" + s + "=" + value;
}
}
return params;
}
}



