/**
 * GET请求
 * 
 * @param url
 *            请求url,参数拼在请求串中
 */
public static String get(String url) {
String responseContent = null;
// 创建默认的httpClient实例.
CloseableHttpClient httpclient = HttpClients.createDefault();
CloseableHttpResponse response = null;
HttpGet httpGet = new HttpGet(url);
Logger.getRootLogger().info("--------------------------------------------");
Logger.getRootLogger().info("GET " + httpGet.getURI());
// 执行get请求.
try {
response = httpclient.execute(httpGet);
HttpEntity entity = response.getEntity();
// 打印响应状态
Logger.getRootLogger().info(response.getStatusLine());
if (entity != null) {
responseContent = EntityUtils.toString(entity);
// 打印响应内容
Logger.getRootLogger().info("Response content: "+ responseContent);
Logger.getRootLogger().info("--------------------------------------------");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
httpclient.close();
response.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return responseContent;
 
}
 
/**
 * 
 * @param url
 * @param paramMap
 */
public static String post(String url, Map<String, String> paramMap) {
String responseContent = null;
// 创建默认的httpClient实例.
CloseableHttpClient httpclient = HttpClients.createDefault();
// 创建httppost
HttpPost httppost = new HttpPost(url);
Logger.getRootLogger().info("--------------------------------------------");
Logger.getRootLogger().info("POST " + httppost.getURI());
// 创建参数队列
List<BasicNameValuePair> formparams = new ArrayList<BasicNameValuePair>();
if (paramMap != null) {
Set<String> key = paramMap.keySet();
for (Iterator<String> it = key.iterator(); it.hasNext();) {
String paramKey = (String) it.next();
formparams.add(new BasicNameValuePair(paramKey, paramMap
.get(paramKey)));
Logger.getRootLogger().info(paramKey+" = "+paramMap.get(paramKey));
}
}
 
UrlEncodedFormEntity uefEntity;
try {
uefEntity = new UrlEncodedFormEntity(formparams, "UTF-8");
httppost.setEntity(uefEntity);
CloseableHttpResponse response = httpclient.execute(httppost);
try {
HttpEntity entity = response.getEntity();
Logger.getRootLogger().info(response.getStatusLine());
if (entity != null) {
responseContent = EntityUtils.toString(entity, "UTF-8");
Logger.getRootLogger().info("Response content: "+ responseContent);
Logger.getRootLogger().info("--------------------------------------------");
}
} finally {
response.close();
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
// 关闭连接,释放资源
try {
httpclient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return responseContent;
}

httpclient调用方法的更多相关文章

  1. httpclient调用webservice接口的方法实例

    这几天在写webservice接口,其他的调用方式要生成客户端代码,比较麻烦,不够灵活,今天学习了一下httpclient调用ws的方式,感觉很实用,话不多说,上代码 http://testhcm.y ...

  2. springMVC、httpClient调用别人提供的接口!!!(外加定时调用)

    import com.ibm.db.util.AppConfig; import com.ibm.db.util.JacksonUitl; import org.apache.http.HttpEnt ...

  3. httpclient 调用WebAPI

    1.创建webapi项目,提供接口方法如下: /// <summary> /// 获取租户.位置下的所有传感器 /// </summary> /// <returns&g ...

  4. httpclient调用https

    httpclient调用https报错: Exception in thread "main" java.lang.Exception: sun.security.validato ...

  5. 通过HttpClient 调用ASP.NET Web API

    在前面两篇文章中我们介绍了ASP.NET Web API的基本知识和原理,并且通过简单的实例了解了它的基本(CRUD)操作.我们是通过JQuery和Ajax对Web API进行数据操作.这一篇我们来介 ...

  6. SpringMVC,MyBatis项目中兼容Oracle和MySql的解决方案及其项目环境搭建配置、web项目中的单元测试写法、HttpClient调用post请求等案例

     要搭建的项目的项目结构如下(使用的框架为:Spring.SpingMVC.MyBatis): 2.pom.xml中的配置如下(注意,本工程分为几个小的子工程,另外两个工程最终是jar包): 其中 ...

  7. C#工具:利用HttpClient调用WebApi

    可以利用HttpClient来进行Web Api的调用.由于WebA Api的调用本质上就是一次普通的发送请求与接收响应的过程, 所有HttpClient其实可以作为一般意义上发送HTTP请求的工具. ...

  8. Asp.Net MVC WebAPI的创建与前台Jquery ajax后台HttpClient调用详解

    1.什么是WebApi,它有什么用途? Web API是一个比较宽泛的概念.这里我们提到Web API特指ASP.NET MVC Web API.在新出的MVC中,增加了WebAPI,用于提供REST ...

  9. 通过HttpClient调用服务

    /** * 通过HttpClient调用服务 * * @param url 路径 * data json数据 * @return */ //post请求方法public String sendItsm ...

随机推荐

  1. Linux服务器rsync自动备份

    一.在 server 端配置 1. 编辑配置文件 #vi /etc/rsyncd.conf 添加下面的配置参数: uid = nobody # 该选项指定当该模块传输文件时守护进程应该具有的uid.默 ...

  2. ACdream 1069 无耻的出题人

    题目翻译完了是每一位之和是多少. #pragma comment(lnker, "/STACK:1024000000,1024000000") #include<cstdio ...

  3. 难以记住的sql语句

    天,把这篇文章转移到这里,增强一下记忆,找起来也更方便. 导出: mysqldump -u username -p password -h hname dbname tblname > file ...

  4. jdbc连接数据库的步骤

    1.加载JDBC驱动程序: 在连接数据库之前,首先要加载想要连接的数据库的驱动到JVM(Java虚拟机), 这通过java.lang.Class类的静态方法forName(String  classN ...

  5. thinkphp 匹配qq 表情

    <?php// 匹配qq表情    function replace_phiz($content){        preg_match_all('/\[.*?\]/is',$content,$ ...

  6. Linux shell 操作 postgresql,并设置crontab任务

    Linux shell 操作 postgresql:删除间隔日期的数据-删除指定日期的数据-vacuumdb 清理数据库 -清理日志 -定期执行脚本 *修改pg_hba.conf 设置本地连接无密码, ...

  7. 简单学习JavaScript面向对象编程

    JavaScript是一种弱类型语言.有一种原型机制. 1.创建一个空对象:var bill = {}; 给这个对象添加属性和方法: bill.name = "Bill E Goat&quo ...

  8. erlang ets表

    一.表遍历 通过ets:first/1获取表的第一个关键字,表中下一个关键字用ets:next/2得到,直到ets:next/2返回'$end_of_table' 当多几个进程并发访问ets表时,可以 ...

  9. submit提交表单后,不刷新当前页面

    <form method="get" target="test" action="a.html"> <input type ...

  10. Foundations of Computer Science

    1, Iteration, Induction and Recursion 2, the running time of program 3, combinatorics and probabilit ...