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. Kazam: a perfect srceen recorder in Linux/Ubuntu

    Kazam provides a well designed and easy to use interface for capturing screencasts and screenshots. ...

  2. 3----lua的数据转换及运算符

    lua的基本数据类型转换 转换成字符串 tostring( ... ) 可以将布尔类型和数字类型的值转换为字符串类型的值 local num=1; print(type(num)) newNum = ...

  3. canvas背景透明

    var can=document.getElementById("canv"); c=can.getContext("2d"); c.globalAlpha=. ...

  4. 关于PHP HTML <input type="file" name="img"/>上传图片,图片大小,宽高,后缀名。

    在我们的系统中,不免要上传图片,视频等文件,在上传中,需要做的一些判断,文件大小等方面. 注意: 在php.ini 中的post_max_size,upload_max_filesize默认为2M,在 ...

  5. $.ajax() ,$.post(),$.get() 的用法

    在编写代码的过程中,我们会用到一些的需要局部刷新,而且需要数据传递的情况,我们通常是有$.ajax() ,$.post(),$.get() 这三种方式. $.ajax()方法通过 HTTP 请求加载远 ...

  6. 最牛叉的街机游戏合集 & 模拟器

    亲爱的小伙伴们,是否还记得那年我们玩的疯狂的街机游戏吗,街机中心提供400多个街机游戏,让你爽到底. 例如:拳皇96,拳皇97,恐龙新世纪.名将.快打旋风.惩罚者.魂斗罗.超级玛丽.雪山兄弟.忍者神龟 ...

  7. Redis主从配置详细过程

    Redis的主从复制功能非常强大,一个master可以拥有多个slave,而一个slave又可以拥有多个slave,如此下去,形成了强大的多级服务器集群架构.下面楼主简单的进行一下配置. 1.上面安装 ...

  8. Win7x64_chromeX86_相关路径

    1. C:\Users\33\AppData\Local\Google 里面有2个文件夹:“Chrome”.“CrashReports” 2. C:\Program Files (x86)\Googl ...

  9. Monkey学习(3)如何在Android模拟器中安装apk

    1.运行SDK Manager,选择模拟器,并运行模拟器,我这里用的是已经配置好的模拟器“RedMI” 2.已启动好的模拟器“RedMI” 3.记住需要安装apk文件的位置,我这里放在了F盘的根目录下 ...

  10. 3 javascript

    3 javascript javascript基础 html: 负责了一个页面的结构.   css: 负责了一个页面的样式.    javascript: 负责与用户进行交互. 1997年欧洲的计算机 ...