/**
 * 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. 语言总结—C/C++

    参考<程序员面试宝典> 1. 基本概念 1.1 赋值语句 例1. 按位与操作,例如:a=3,b=3,a&b值等于 0011 & 0011 结果还是0011,那么值还是3: ...

  2. html中 iframe子页面 与父页面之间的方法调用 ;

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  3. Activity的生命周期和启动模式

    Activity的生命周期分析 典型情况下的生命周期.是指在用户参与的情况下,Activity所经过的生命周期的改变. 异常情况下的生命周期.是指Activity被系统回收或者由于当前设备的Confi ...

  4. Effective JavaScript :第二章

    1.熟练掌握闭包 理解闭包要学会三个基本的事实: ①JavaScript允许你引用在当前函数以外定义的变量: 例如: function makeSandwich(){ var magicIngredi ...

  5. 使用 Spark MLlib 做 K-means 聚类分析[转]

    原文地址:https://www.ibm.com/developerworks/cn/opensource/os-cn-spark-practice4/ 引言 提起机器学习 (Machine Lear ...

  6. c# propertyGrid下拉选项

    实现下面效果的propertygrid属性下拉选择

  7. android基础(一)

    wrap_conten:包裹实际文本内容 match_parent:当前控件铺满父类容器:2.3api之后添加的一个属性值 fill_parent:包裹实际文本内容,在2.3api之前的一个属性值 a ...

  8. ggplot2 theme相关设置—矩形设置(rect)

    在主题设置中,rect设置主要用于设置图例和面板 element_rect(fill = NULL, colour = NULL, size = NULL, linetype = NULL, colo ...

  9. JavaEE XML 基础知识

    JavaEE XML 基础知识 @author ixenos 1.    XML开头都需要一个声明 <?和?>表明这是一个处理指令 <?xml version=”1.0” encod ...

  10. Codeforces Round #366 (Div. 2)_C. Thor

    C. Thor time limit per test 2 seconds memory limit per test 256 megabytes input standard input outpu ...