httpClient 4.x post get方法
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方法的更多相关文章
- 【JAVA】通过HttpClient发送HTTP请求的方法
HttpClient介绍 HttpClient 不是一个浏览器.它是一个客户端的 HTTP 通信实现库.HttpClient的目标是发 送和接收HTTP 报文.HttpClient不会去缓存内容,执行 ...
- 使用HttpClient进行http post/get方法的调用,以及使用dom4j解析xml
import java.io.IOException; import java.util.List; import javax.servlet.ServletInputStream; import j ...
- httpclient请求服务的各种方法实例
<!--话不多说,直接上代码--> import com.csis.ConfigManagerimport com.csis.io.web.DefaultConfigItemimport ...
- 基于apache httpclient的常用接口调用方法
现在的接口开发,大部分是基于http的请求和处理,现在整理了一份常用的调用方式工具类 package com.xh.oms.common.util; import java.io.BufferedRe ...
- httpclient调用webservice接口的方法实例
这几天在写webservice接口,其他的调用方式要生成客户端代码,比较麻烦,不够灵活,今天学习了一下httpclient调用ws的方式,感觉很实用,话不多说,上代码 http://testhcm.y ...
- Android 6.0删除Apache HttpClient相关类的解决方法
相应的官方文档如下: 上面文档的大致意思是,在Android 6.0(API 23)中,Google已经移除了Apache HttpClient相关的类,推荐使用HttpUrlConnection. ...
- .NET Core 2.0 httpclient 请求卡顿解决方法
var handler = new HttpClientHandler() { AutomaticDecompression = DecompressionMethods.GZip,UseProxy ...
- android中HttpClient的应用(POST方法)
首先在http://hc.apache.org/downloads.cgi下载HttpClient包 直接看代码 import android.os.Handler; import android.o ...
- Android HttpClient基本使用方法
GET 方式 //先将参数放入List,再对参数进行URL编码 List<BasicNameValuePair> params = new LinkedList<BasicNameV ...
随机推荐
- STORM_0001_用vmware拷贝出三个相同的ubuntu搭建小的zookeeper集群
第一次配置zookeeper的集群 因为想运行storm必须搭建集群在自己的电脑上拷贝了自己的ubuntu虚拟机采用的是vmware给虚拟机分配的地址三个机器的配置基本上一样除了myid这个文件看了这 ...
- 【网摘】DICOM 基础简介
一 什么是DICOM?DICOM是Digital Imaging and Communication of Medicine的缩写,是美国放射学会(American College of Radiol ...
- 深入理解 C# 协变和逆变
msdn 解释如下: “协变”是指能够使用与原始指定的派生类型相比,派生程度更大的类型. “逆变”则是指能够使用派生程度更小的类型. 解释的很正确,大致就是这样,不过不够直白. 直白的理解: “协变” ...
- JMS【三】--ActiveMQ简单的HelloWorld实例
第一篇博文JMS[一]--JMS基本概念,我们介绍了JMS的两种消息模型:点对点和发布订阅模型,以及消息被消费的两个方式:同步和异步,JMS编程模型的对象,最后说了JMS的优点. 第二篇博文JMS[二 ...
- iOS - OC 术语表
1.术语表
- c trans
#define BUFSIZE 100 char buf[BUFSIZE]; ; int getch(void) { )? buf[--bufp] : getchar(); } void ungetc ...
- 如何让Service自动重启而不被kill掉
重写service的onStartCommand方法. @Override public int onStartCommand(Intent intent, int flags, int sta ...
- Android 随想录之 Android 系统架构
应用层(Application) Android 的应用层由运行在 Android 设备上的所有应用程序共同构成(系统预装程序以及第三方应用程序). 系统预装应用程序包含拨号软件.短信.联系人.邮件客 ...
- NowCoder猜想(素数筛法+位压缩)
在期末被各科的大作业碾压快要窒息之际,百忙之中抽空上牛客网逛了逛,无意中发现一道好题,NowCoder猜想,题意很明显,就是个简单的素数筛法,但竟然超内存了,我晕(+﹏+)~ 明明有 3 万多 k ...
- nginx系统真正有效的图片防盗链完整设置详解
原文:http://www.wufangbo.com/nginx-fang-dao-lian/ 关于nginx防盗链的方法网上有很多教程,都可以用,但是我发现很多教程并不完整,所做的防盗链并不是真正的 ...