public static String doPost(String url, String encoding, String contentType, String sendData)

throws Exception {

HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();

CloseableHttpClient httpclient = httpClientBuilder.build();

HttpPost httppost = new HttpPost(url);

StringEntity myEntity = new StringEntity(sendData, encoding);

myEntity.setContentType(contentType);

httppost.setEntity(myEntity);

HttpResponse response = httpclient.execute(httppost);

HttpEntity resEntity = response.getEntity();

InputStreamReader reader = new InputStreamReader(resEntity.getContent(), encoding);

char[] buff = new char['Ѐ'];

StringBuilder sb = new StringBuilder();

int length;

while ((length = reader.read(buff)) != -1) {

sb.append(new String(buff, 0, length));

}

httpclient.close();

return sb.toString();

}

public static void requestGet(String urlWithParams) throws Exception {

CloseableHttpClient httpclient = HttpClientBuilder.create().build();

// HttpGet httpget = new HttpGet("http://www.baidu.com/");

HttpGet httpget = new HttpGet(urlWithParams);

// 配置请求的超时设置

RequestConfig requestConfig = RequestConfig.custom().setConnectionRequestTimeout(5000).setConnectTimeout(5000)

.setSocketTimeout(5000).build();

httpget.setConfig(requestConfig);

CloseableHttpResponse response = httpclient.execute(httpget);

System.out.println("StatusCode -> " + response.getStatusLine().getStatusCode());

HttpEntity entity = response.getEntity();

String jsonStr = EntityUtils.toString(entity);// , "utf-8");

System.out.println(jsonStr);

httpget.releaseConnection();

}

httpClient 4.x post get方法的更多相关文章

  1. 【JAVA】通过HttpClient发送HTTP请求的方法

    HttpClient介绍 HttpClient 不是一个浏览器.它是一个客户端的 HTTP 通信实现库.HttpClient的目标是发 送和接收HTTP 报文.HttpClient不会去缓存内容,执行 ...

  2. 使用HttpClient进行http post/get方法的调用,以及使用dom4j解析xml

    import java.io.IOException; import java.util.List; import javax.servlet.ServletInputStream; import j ...

  3. httpclient请求服务的各种方法实例

    <!--话不多说,直接上代码--> import com.csis.ConfigManagerimport com.csis.io.web.DefaultConfigItemimport ...

  4. 基于apache httpclient的常用接口调用方法

    现在的接口开发,大部分是基于http的请求和处理,现在整理了一份常用的调用方式工具类 package com.xh.oms.common.util; import java.io.BufferedRe ...

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

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

  6. Android 6.0删除Apache HttpClient相关类的解决方法

    相应的官方文档如下: 上面文档的大致意思是,在Android 6.0(API 23)中,Google已经移除了Apache HttpClient相关的类,推荐使用HttpUrlConnection. ...

  7. .NET Core 2.0 httpclient 请求卡顿解决方法

    var handler = new HttpClientHandler() { AutomaticDecompression = DecompressionMethods.GZip,UseProxy ...

  8. android中HttpClient的应用(POST方法)

    首先在http://hc.apache.org/downloads.cgi下载HttpClient包 直接看代码 import android.os.Handler; import android.o ...

  9. Android HttpClient基本使用方法

    GET 方式 //先将参数放入List,再对参数进行URL编码 List<BasicNameValuePair> params = new LinkedList<BasicNameV ...

随机推荐

  1. Ubuntu下手动安装VMware Tools步骤

    To mount the CD image and extract the contents: Power on the virtual machine. Log in to the virtual ...

  2. 从POI到O2O 看百度地图如何走出未来之路

    近期O2O的烧钱融资大战如火如荼,有人已经把O2O大战,用乌合之众的群体心理失控来形容.其实厂商都不傻,O2O烧钱大家都知道,但是大家还知道O2O背后这块大蛋糕价值"万亿级". 有 ...

  3. Python学习(22)python网络编程

    Python 网络编程 Python 提供了两个级别访问的网络服务.: 低级别的网络服务支持基本的 Socket,它提供了标准的 BSD Sockets API,可以访问底层操作系统Socket接口的 ...

  4. Mysql delete,truncate,drop

    1.delete 是DML(Data Manipulation Language),每次删除一行,作为事务记录在日志,可以回滚.delete from xxx 2.truncate是DDL(Data ...

  5. [mysql]支持emoji(字符集问题)!

    问题的根源 主要问题就是在字符集,一般解决这种问题都是靠试验.我实验了一通,得出的结论和大家分享一下(如有错误,还望指正): 数据库的字符集 数据库连接的字符集 配置方法 设置数据库的字符集为utf8 ...

  6. white-space: nowrap

    CSS:需要加上宽度(width:100px).超出隐藏(overflow:hidden;).强制在同一行显示(white-space: nowrap;).省略号(text-overflow:elli ...

  7. go语言中间的循环

    在Go语言中只有很少的几个控制结构,它没有while或者do-while循环. 但是它有for.switch.if.而且switch接受像for那样可选的初始化语句.下面来认识一下他们 一.if语句 ...

  8. PC上安装多个操作系统

    目 录 第1章 绪论    1 1.1 目标    1 1.2 适宜的读者    1 第2章 制作启动U盘    2 2.1 初级安装    2 2.2 启动分析    3 2.3 高级安装    1 ...

  9. MySQL内核深度优化

    版权声明:本文由简怀兵原创文章,转载请注明出处: 文章原文链接:https://www.qcloud.com/community/article/179 来源:腾云阁 https://www.qclo ...

  10. MySQL中基本的多表连接查询教程

    一.多表连接类型1. 笛卡尔积(交叉连接) 在MySQL中可以为CROSS JOIN或者省略CROSS即JOIN,或者使用','  如: SELECT * FROM table1 CROSS JOIN ...