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. SQL Server 临时表 Vs 表变量

    开始 说临时表和表变量,这是一个古老的话题,我们在网上也找到很多的资料阐述两者的特征,优点与缺点.这里我们在SQL Server 2005\SQL Server 2008版本上通过举例子,说明临时表和 ...

  2. Java 实现阶乘算法

    阶乘算法如下: 以下列出 0 至 20 的阶乘: 0!=1,(0 的阶乘是存在的) 1!=1, 2!=2, 3!=6, 4!=24, 5!=120, 6!=720, 7!=5040, 8!=40320 ...

  3. HLS播放权限测试记录

    阿里云: http://live.mudu.tv/watch/11y1at.m3u8 斗鱼加密: http://hls3a.douyucdn.cn/live/485503r63zGiPn4D_550/ ...

  4. box-shadow使用指南

    Summary The box-shadow property describes one or more shadow effects as a comma-separated list. It e ...

  5. 查看package编译时的错误信息及重新编译

    开发时,一般都是使用PL/SQL工具进行开发,查看编译错误及重新编译都很简单,但是一般的生产环境是不允许连接外界工具的,只能在命令行中进行重新编译及查看,今天我就遇到了这个问题,现在总结如下: 1.获 ...

  6. (二)再议MII、RMII、GMII接口

    概述:         MII (Media Independent Interface(介质无关接口)或称为媒体独立接口,它是IEEE-802.3定义的以太网行业标准.它包括一个数据接口和一个MAC ...

  7. JavaWeb网页聊天室(WebSocket即时通讯)

    原文:http://baike.xsoftlab.net/view/656.html Git地址 http://git.oschina.net/loopcc/WebSocketChat 概要: Web ...

  8. 转:关于C++14:你需要知道的新特性

    关于C++14:你需要知道的新特性 遇见C++ Lambda C++14 lambda 教程 C++11 lambda表达式 C++标准库:使用 std::for_each std::generate ...

  9. 使用火狐的restclient发送http接口post及get请求

    1.在firefox安装restclient插件,具体参照http://jingyan.baidu.com/article/1876c8529b07e3890b137623.html: —.发送pos ...

  10. C#_微信支付V3

    基础问题先排除: 确保 商户功能 审核通过,会有官方邮件 支付授权目录(注意看文档,大小写关系很大 点击支付按钮,提示“access_denied” 网上有很多关于此问题的解决) 点击支付按钮,提示“ ...