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. Webbrowser控件判断网页加载完毕的简单方法 (转)

    摘自:http://blog.csdn.net/cometnet/article/details/5261192 一般情况下,当ReadyState属性变成READYSTATE_COMPLETE时,W ...

  2. myeclipse10.05安装aptana

    安装步骤: 1.下载aptana3.2 Eclipse Plugin插件. 下载地址:http://update1.aptana.org/studio/3.2/024747/index.html 2. ...

  3. 使用Memory Analyzer tool(MAT)分析内存泄漏

    前言的前言 写blog就是好,在大前提下可以想说什么写什么,不像投稿那么字字斟酌.上周末回了趟成都办事,所以本文来迟了.K117从达州经由达成线往成都方向走的时候,发现铁路边有条河,尽管我现在也不知道 ...

  4. shiro-web整合

    1.所需要jar <!-- shiro核心包 --> <dependency> <groupId>org.apache.shiro</groupId> ...

  5. SQL 总结

    1. select 使用正则表达式 正则表达式的模式串, 与linux基本相同, oracle提供以下4个函数来支持正则表达式: REGEXP_LIKE: 比较一个字符串是否与正则表达式匹配(看来是返 ...

  6. ORACLE数据库DBMS_JOB的创建与使用

    http://my.oschina.net/u/2309120/blog/371437 创建 DBMS_JOB 使用以下语句: VARIABLE jobno number;begin DBMS_JOB ...

  7. 关于js的兼容问题(小办法)!

    今天整理了一下浏览器对JS的兼容问题,希望能给你们带来帮助,我没想到的地方请留言给我,我再加上: 常遇到的关于浏览器的宽高问题: //以下均可console.log()实验 var winW=docu ...

  8. javascript------>(此文转发)

    JS控制div跳转到指定的位置的解决方案总结   总结一下自己在写这个需求遇到的问题,相信大家应该是经常遇到的.即要求滚轮滚动到指定的位置.先看下基本的解决方案. 1.给链接a加个#的方式来实现跳转. ...

  9. 选择排序算法Java与Python实现

    Java 实现 package common; public class SimpleArithmetic { /** * 选择排序 * 输入整形数组:a[n] [4.5.3.7] * 1. 取数组编 ...

  10. java中的堆内存和栈内存

    Java把内存分成两种: 一种叫做栈内存 一种叫做堆内存 栈内存 : 在函数中定义的一些基本类型的变量和对象的引用变量都是在函数的栈内存中分配.当在一段代码块中定义一个变量时,java就在栈中为这个变 ...