/**
 * 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. Spire.XLS 在程序中直接打印excel

    上代码 if (tbPrintSetBindingSource.Current == null) return; var item_TbPrintSet = tbPrintSetBindingSour ...

  2. Python 2.X-关于函数返回的数值类型

    在使用同一个函数,相同的参数的时候,参数在传递的过程中使用了不同的形式(有无小数点)决定了该函数返回的值的类型. # -*- coding:utf-8 -*- def return_types(one ...

  3. ssh无密码登录设置方法以及出现问题 ECDSA host key 和IP地址对应的key不同的解决

    最近在做hadoop,因为要求各主机之间的用户必须相同,且为方便远程登录,需配置无密码登录 先附上ssh无密码登录设置方法: 先生成密钥并配置无ssh无密码登录本机,输入命令: ssh-keygen ...

  4. angularjs的forEach使用

    最近一直在写angularjs中的http后台数据交互,存在的问题就是数据传输中数据格式的问题,如何将传输过来的数据转化为自己需要的数据.当然如果你会一点后台语言的话,完全可以用在后台把数据转化为需要 ...

  5. English Vocabulary

    Creative - producing or using original and unusual ideas Computer literate - familiarity with comput ...

  6. 常见编程语言对REPL支持情况小结[转]

    文章转载自http://www.nowamagic.net/librarys/veda/detail/2462 最近跟一个朋友聊起编程语言的一些特性,他有个言论让我略有所思:“不能REPL的都是渣”. ...

  7. map 理解

    键值对 map会将同名的值覆盖掉 public static void main(String[] args) { Map<String,String> maptest=new HashM ...

  8. WCF、Web API、WCF REST、Web Service的区别

    Difference between WCF and Web API and WCF REST and Web Service   The .Net framework has a number of ...

  9. mnesia

    1.模式创建 mnesia:create_schema([node()|nodes()]).集群的节点之间创建模式 2.启动和停止 application:start(mnesia). applica ...

  10. 2.编写IoDemo.java的Java应用程序,程序完成的功能是:首先读取text.txt文件内容,再通过键盘输入文件的名称为iodemo.txt,把text.txt的内容存入iodemo.txt

    package zuoye; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; ...