org.apache.commons.httpclient
2024-09-29 16:14:57
org.apache.commons.httpclient
/**
* post 方法
* @param url
* @param params
* @return
*/
public static String post(String url, Object content, String encode) throws Exception { byte[] responseBody = null;
HttpClient httpclient = new HttpClient();
PostMethod httpPost = new PostMethod(url);
// 设置连接超时时间(单位毫秒)
httpclient.getHttpConnectionManager().getParams().setConnectionTimeout(60000);
// 设置读数据超时时间(单位毫秒)
httpclient.getHttpConnectionManager().getParams().setSoTimeout(60000);
try {
httpPost.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,new DefaultHttpMethodRetryHandler(3, false));
// servlet
if (content instanceof Map) {
@SuppressWarnings("unchecked")
Map<String, String> map = (Map<String, String>)content;
NameValuePair[] param = new NameValuePair[map.size()]; int index = 0;
for (Map.Entry<String, String> entry : map.entrySet()) {
param[index] = new NameValuePair(entry.getKey(),URLEncoder.encode(entry.getValue(), "GBK"));
} httpPost.setRequestBody(param);
}
// rest
else {
httpPost.setRequestEntity(new StringRequestEntity((String)content,"plain/text", encode));
} // post
int statusCode = httpclient.executeMethod(httpPost);
// success
if (statusCode == HttpStatus.SC_OK) {
responseBody = httpPost.getResponseBody();
}
// failure
else { }
} catch (HttpException e) {
throw new Exception(e.getMessage());
} catch (IOException e) {
throw new Exception(e.getMessage());
} catch (Exception e) {
throw new Exception(e.getMessage());
} finally {
httpPost.releaseConnection();
}
return new String(responseBody, encode);
}
最新文章
- 从贝叶斯到粒子滤波——Round 1
- 关于gdb和shp的FID问题
- javascript对象创建方式
- js动画之获取元素属性
- RDF和Jena RDF API简介
- bcd 8421码
- 【leetcode❤python】119. Pascal&#39;s Triangle II
- Poj 1029 分类: Translation Mode 2014-04-04 10:18 112人阅读 评论(0) 收藏
- HDU 5734 Acperience (推导)
- 基于ActiveMQ的统一日志服务
- css笔记13:display用法
- uCGUI窗口初始化过程
- 随记一个C的毫秒级群PING
- Java基础知识二次学习--第七章 容器
- [Swift]LeetCode1029. 两地调度 | Two City Scheduling
- 业务-----修改Service常用逻辑
- Ldap-crack-test?
- 758B Blown Garland
- 北京市基本医疗保险A类定点医疗机构名单(2010-09-29)
- EPANET头文件解读系列7——MEMPOOL.H