// 创建默认的httpclient实例
CloseableHttpClient httpclient = getHttpClient();
CloseableHttpResponse response = null;
// 创建httpPost
HttpPost httpPost = new HttpPost("url地址");
httpPost.setHeader(HTTP.CONTENT_TYPE, "application/json; charset=utf-8");
try {
StringEntity param = new StringEntity(params, ContentType.APPLICATION_JSON);
httpPost.setEntity(param);
response = httpclient.execute(httpPost);
HttpEntity entity = response.getEntity();
if(entity != null){
InputStream is = entity.getContent(); } } catch (Exception e) {
LOG.error("sendPostRequest error:", e);
} finally {
try {
//关闭连接,释放资源
response.close();
httpclient.close();
} catch (Exception e) {
LOG.error("HttpClient close error:", e);
}
}

发送Get请求

CloseableHttpClient httpclient = getHttpClient();
CloseableHttpResponse response = null;
HttpGet httpget = new HttpGet("url地址");
try {
response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();
if(entity != null){
InputStream is = entity.getContent();
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
//关闭连接,释放资源
response.close();
closeHttpClient(httpclient);
} catch (Exception e) {
LOG.error("HttpClient close error:", e);
}
}

借用某位大神的代码,只为日后方便查阅,请勿介意。

HttpClient发送Post请求,get请求的更多相关文章

  1. HttpClient发送get,post接口请求

    HttpClient发送get post接口请求/*  * post  * @param url POST地址 * @param data POST数据NameValuePair[] * @retur ...

  2. Android笔记---使用HttpClient发送POST和GET请求

    在Android上发送 HTTP 请求的方式一般有两种, HttpURLConnection 和 HttpClient,关于HttpURLConnection的使用方法能够參考HTTP之利用HttpU ...

  3. Java实现HttpClient发送GET、POST请求(https、http)

    1.引入相关依赖包 jar包下载:httpcore4.5.5.jar    fastjson-1.2.47.jar maven: <dependency> <groupId>o ...

  4. java apache commons HttpClient发送get和post请求的学习整理(转)

    文章转自:http://blog.csdn.net/ambitiontan/archive/2006/01/06/572171.aspx HttpClient 是我最近想研究的东西,以前想过的一些应用 ...

  5. 使用httpclient发送get或post请求

    HttpClient 是 Apache Jakarta Common 下的子项目,可以用来提供高效的.最新的.功能丰富的支持 HTTP 协议的客户端编程工具包,并且它支持 HTTP 协议最新的版本和建 ...

  6. HttpClient发送Get和Post请求

    package JanGin.httpClient.demo; import java.io.IOException; import java.io.UnsupportedEncodingExcept ...

  7. HttpClient 发送 HTTP、HTTPS 请求的简单封装

    import org.apache.commons.io.IOUtils; import org.apache.http.HttpEntity; import org.apache.http.Http ...

  8. [java,2018-01-16] HttpClient发送、接收 json 请求

    最近需要用到许多在后台发送http请求的功能,可能需要发送json和xml类型的数据. 就抽取出来写了一个帮助类: 首先判断发送的数据类型是json还是xml: import org.dom4j.Do ...

  9. 读取配置文件的URL,使用httpClient发送Post和Get请求,实现查询快递物流和智能机器人对话

    1.主要jar包: httpclient-4.3.5.jar   httpcore-4.3.2.jar 2.目录结构如图所示: 3.url.properties文件如下: geturl=http:// ...

  10. 【httpclient-4.3.1.jar】httpclient发送get、post请求以及携带数据上传文件

    1.发送get.post携带参数以及post请求接受JSON数据: package cn.qlq.utils; import java.io.BufferedReader; import java.i ...

随机推荐

  1. php版本的选择

    简单来说non-thread-safe 非 线程安全 与IIS 搭配环境,thread-safe 线程安全 与apache 搭配的 环境这个大家一定要注意,否则用错了版本,apache是无法启动的,另 ...

  2. Python实战之IO多路复用select实例

    Select方法: 句柄列表11, 句柄列表22, 句柄列表33 = select.select(句柄序列1, 句柄序列2, 句柄序列3, 超时时间)   参数: 可接受四个参数(前三个必须) 返回值 ...

  3. python 发送邮件,未完

    def send_mail(): try: print "send mail..." # handle = smtplib.SMTP('smtp.163.com', 25) # h ...

  4. cookie存储中文

    写cookie         Cookie   chineseCookie   =   new   Cookie( "chineseCookie ",   URLEncoder. ...

  5. DevOps教程

    唠叨话 关于德语关我屁事与靠计算逼哥数据,知识点的教学教程. 先简要搭建知识点框架:后逐步完善知识点内容.(暂时提供知识点,大部分未完善,持续更新中.) 注:第一版本,结束于2017年10月18日.其 ...

  6. PHP二维关联数组的遍历方式

    采用foreach循环对二维索引数组进行遍历,相对来讲速度更快,效率更高,foreach循环是PHP中专门用来循环数组的.实例也相对简单,多加练习,想清楚程序运行逻辑即可. <?php $arr ...

  7. exp3.1实现顺序栈的各种操作

    #include<iostream>using namespace std;#include<malloc.h>typedef char Elem;typedef struct ...

  8. C++大数精度计算(带小数点)

    转: (原出处不可考,若有侵权,请联系我立即删除) 头文件: // WTNumber.h: interface for the CWTNumber class. // //////////////// ...

  9. winform中执行任务,解决未响应界面

    private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)        {            var coun ...

  10. LeetCode 119. Pascal's Triangle II (杨辉三角之二)

    Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Return [1,3, ...