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);
}

最新文章

  1. 从贝叶斯到粒子滤波——Round 1
  2. 关于gdb和shp的FID问题
  3. javascript对象创建方式
  4. js动画之获取元素属性
  5. RDF和Jena RDF API简介
  6. bcd 8421码
  7. 【leetcode❤python】119. Pascal&#39;s Triangle II
  8. Poj 1029 分类: Translation Mode 2014-04-04 10:18 112人阅读 评论(0) 收藏
  9. HDU 5734 Acperience (推导)
  10. 基于ActiveMQ的统一日志服务
  11. css笔记13:display用法
  12. uCGUI窗口初始化过程
  13. 随记一个C的毫秒级群PING
  14. Java基础知识二次学习--第七章 容器
  15. [Swift]LeetCode1029. 两地调度 | Two City Scheduling
  16. 业务-----修改Service常用逻辑
  17. Ldap-crack-test?
  18. 758B Blown Garland
  19. 北京市基本医疗保险A类定点医疗机构名单(2010-09-29)
  20. EPANET头文件解读系列7——MEMPOOL.H

热门文章

  1. php 配置本地自定义域名
  2. javascript学习之JSON
  3. 修改了系统自带头文件后,Xcode会报错
  4. linux 硬件信息
  5. Three.js基础探寻十——动画
  6. iOS中scrollview是否要回弹
  7. C++Primer 第十七章
  8. Linux C进程内存布局
  9. 构造器Constructor是否可被override?
  10. bzoj 4237稻草人