<groupId>commons-httpclient</groupId>
<artifactId>commons-httpclient</artifactId>
<version>3.1</version>
</dependency>
/**
* @Title: postRequestResponseBodyAsString
* @Description: http post请求,application/x-www-form-urlencoded方式
* @param url
* @param parameters
* @param contimeout
* @param sotimeout
* @return
* @throws Exception
* @throws
*/
public static String postRequestResponseBodyAsString(String url, Map<String, String> parameters,Integer contimeout,Integer sotimeout) throws Exception {
HttpClient httpClient = new HttpClient();
httpClient.getHttpConnectionManager().getParams().setSoTimeout(sotimeout);
httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(contimeout);
// httpClient.getHostConfiguration().setProxy("proxy2.fn.com",8080);
PostMethod method = new PostMethod(url);
int statusCode;
String responseCharSet ="";
String responseString = "";
try {
for (String key : parameters.keySet()) {
method.addParameter(key, parameters.get(key));
}
method.addRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
statusCode = httpClient.executeMethod(method);
if (statusCode != HttpStatus.SC_OK) {
LOG.error(">>>>>>>>>>>>>>> Method failed: " + method.getStatusLine());
LOG.error(">>>>>>>>>>>>>>> Http服务链路异常:服务器状态码为" + statusCode);
}
responseCharSet = method.getResponseCharSet();
responseString = method.getResponseBodyAsString();
if ("ISO-8859-1".equals(responseCharSet)) {
responseString = new String(responseString.getBytes(responseCharSet), "UTF-8");
}
} catch (Exception e) {
LOG.error(">>>>>>>>>>>>>>> Http服务链路异常:" + e.getMessage() + e);
} finally {
method.releaseConnection();
}
return responseString;
}
public static String postRequestResponseBodyAsString(String url, String str,Integer contimeout,Integer sotimeout) throws Exception{
HttpClient httpClient = new HttpClient();
httpClient.getHttpConnectionManager().getParams().setSoTimeout(sotimeout);
httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(contimeout);
PostMethod method = new PostMethod(url);
int statusCode;
String responseCharSet ="";
String responseString = "";
try {
// String json = JSON.toJSONString(o);
StringRequestEntity entity = new StringRequestEntity(str,"application/json","utf-8");//解决中文乱码问题
method.setRequestEntity(entity);
method.addRequestHeader("Content-Type", "application/json;charset=UTF-8");
statusCode = httpClient.executeMethod(method);
if (statusCode != HttpStatus.SC_OK) {
LOG.error(">>>>>>>>>>>>>> Method failed: " + method.getStatusLine());
LOG.error(">>>>>>>>>>>>>> Http服务链路异常:服务器状态码为" + statusCode);
}
responseCharSet = method.getResponseCharSet();
responseString = method.getResponseBodyAsString();
if ("ISO-8859-1".equals(responseCharSet)) {
responseString = new String(responseString.getBytes(responseCharSet), "UTF-8");
}
} catch (Exception e) {
LOG.error(">>>>>>>>>>>>>> Http服务链路异常:" + e.getMessage() + e);
} finally {
method.releaseConnection();
}
return responseString;
}
/**
* @Title: uploadFile
* @Description: 上传文件
* @param file 文件
* @param url 上传地址
* @param map header-type
* @return
* @throws
*/
public static String uploadFile(File file, String url,Map<String, String> map) {
if (!file.exists()) {
return null;
}
PostMethod postMethod = new PostMethod(url);
String responseCharSet ="";
String responseString = "";
try {
// FilePart:用来上传文件的类
FilePart fp = new CustomFilePart("filedata", file);
Part[] parts = { fp };
postMethod.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET,FilePart.DEFAULT_CHARSET);
// 对于MIME类型的请求,httpclient建议全用MulitPartRequestEntity进行包装
MultipartRequestEntity mre = new MultipartRequestEntity(parts,
postMethod.getParams());
postMethod.setRequestEntity(mre);
for (String key : map.keySet()) {
postMethod.addRequestHeader(key, map.get(key));
}
HttpClient httpClient = new HttpClient();
httpClient.getHttpConnectionManager().getParams().setSoTimeout(20000);
httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(8000);
int status = httpClient.executeMethod(postMethod);
responseCharSet = postMethod.getResponseCharSet();
responseString = postMethod.getResponseBodyAsString();
if (status == HttpStatus.SC_OK) {
if ("ISO-8859-1".equals(responseCharSet)) {
responseString = new String(responseString.getBytes(responseCharSet), "UTF-8");
}
} else {
LOG.error(">>>>>>>>>>>>>> Method failed: " + postMethod.getStatusLine());
LOG.error(">>>>>>>>>>>>>> Http服务链路异常:服务器状态码为" + status);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
// 释放连接
postMethod.releaseConnection();
}
return responseString;
}
/**
* @Title: getFile
* @Description: 拿到文件内容
* @param url 地址
* @param charset 编码
* @return
* @throws
*/
public static String getFile(String url,Charset charset) {
return getFile(url, charset,8000,20000);
}
public static String getFile(String url,Charset charset,Integer contimeout,Integer sotimeout) {
GetMethod method = new GetMethod(url);
String responseCharSet ="";
String responseString = "";
try {
HttpClient httpClient = new HttpClient();
httpClient.getHttpConnectionManager().getParams().setSoTimeout(sotimeout);
httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(contimeout);
int status = httpClient.executeMethod(method);
responseCharSet = method.getResponseCharSet();
responseString = method.getResponseBodyAsString();
if (status == HttpStatus.SC_OK) {
if ("ISO-8859-1".equals(responseCharSet)) {
responseString = new String(responseString.getBytes(responseCharSet), charset);
}
} else {
LOG.error(">>>>>>>>>>>>>> Method failed: " + method.getStatusLine());
LOG.error(">>>>>>>>>>>>>> Http服务链路异常:服务器状态码为" + status);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
// 释放连接
method.releaseConnection();
}
return responseString;
}