org.apache.httpcomponents.httpclient
apache org doc :http://hc.apache.org/httpcomponents-client-ga/tutorial/html/fundamentals.html#d5e49
依赖: <!-- https://mvnrepository.com/artifact/log4j/log4j -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency> <!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpmime -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpmime</artifactId>
<version>4.5</version>
</dependency> <!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5</version>
</dependency> <!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpcore -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
<version>4.4.1</version>
</dependency> <!-- https://mvnrepository.com/artifact/commons-codec/commons-codec -->
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.3</version>
</dependency> <!-- https://mvnrepository.com/artifact/commons-logging/commons-logging -->
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.1.1</version>
</dependency>
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.nio.charset.StandardCharsets;
import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import javax.net.ssl.SSLContext; import org.apache.http.client.config.RequestConfig;
import org.apache.http.conn.ssl.NoopHostnameVerifier;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.ssl.SSLContexts;
import org.apache.http.ssl.TrustStrategy;
import org.apache.log4j.Logger;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.util.EntityUtils;
import org.apache.http.HttpStatus;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost; import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map; public class HttpDemo {
private static Logger LOGGER = Logger.getLogger(HttpDemo.class);
private static CloseableHttpResponse response;
private static HttpEntity entity; /**
* this is ssl ignore https CloseableClient
*/
static CloseableHttpClient sslIgnoreClient() throws KeyStoreException, NoSuchAlgorithmException, KeyManagementException {
CloseableHttpClient client = null;
SSLContext sslContext = SSLContexts.custom().loadTrustMaterial(null, new TrustStrategy() { public boolean isTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {
return true;
}
}).build();
client = HttpClients.custom().setSSLContext(sslContext).setSSLHostnameVerifier(new NoopHostnameVerifier()).build(); return client;
} /**
* http get method
*/
static String doGet(String url, Map<Object, Object> header) throws KeyStoreException, IOException,
KeyManagementException, NoSuchAlgorithmException {
String result = null; // initialize result
CloseableHttpClient request = sslIgnoreClient();
HttpGet msg = new HttpGet(url);
if (header != null) {
header.forEach((key, value) -> {
msg.addHeader(key.toString(), value.toString());
}); }
response = request.execute(msg);
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
entity = response.getEntity();
result = EntityUtils.toString(entity, "UTF-8");
}
return result;
} /**
* String body is json type
*/
static String doPost(String url, String body, Map<Object, Object> header) throws KeyStoreException, IOException,
KeyManagementException, NoSuchAlgorithmException {
String result = null;
CloseableHttpClient request = sslIgnoreClient();
HttpPost msg = new HttpPost(url);
if (header != null) {
header.forEach((key, value) -> {
msg.addHeader(key.toString(), value.toString());
});
}
msg.setEntity(new StringEntity(body));
response = request.execute(msg);
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
entity = response.getEntity();
result = EntityUtils.toString(entity, "UTF-8");
}
return result;
} /**
* do post form-data
*/
static String doPostParam(String url, Map<Object, Object> header, Map<Object, Object> params) throws KeyStoreException, IOException,
KeyManagementException, NoSuchAlgorithmException {
String result = null;
CloseableHttpClient request = sslIgnoreClient();
HttpPost msg = new HttpPost();
if (header != null) {
header.forEach((key, value) -> {
msg.addHeader(key.toString(), value.toString());
});
}
List<BasicNameValuePair> paramList = new ArrayList<>();
params.forEach((key, value) -> {
paramList.add(new BasicNameValuePair(key.toString(), value.toString()));
});
HttpEntity formEntity = new UrlEncodedFormEntity(paramList, "utf-8");
msg.setEntity(formEntity);
response = request.execute(msg);
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
entity = response.getEntity();
result = EntityUtils.toString(entity, "UTF-8");
}
return result;
} /**
* do upload and download operation
*/
static String postFileImage(String url, Map<Object, Object> textBody, List<String> fileList, Map<Object, Object> header, int ttl) throws KeyStoreException, IOException,
KeyManagementException, NoSuchAlgorithmException {
String result = null;
CloseableHttpClient request = sslIgnoreClient();
HttpPost msg = new HttpPost();
//msg.setHeader(key,value);
if (header != null) {
header.forEach((key, value) -> {
msg.addHeader(key.toString(), value.toString());
});
}
RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(ttl).setSocketTimeout(ttl).build();
msg.setConfig(requestConfig);
MultipartEntityBuilder builder = MultipartEntityBuilder.create(); fileList.forEach(item -> {
File f = new File(item);
try {
builder.addBinaryBody(
"file",
new FileInputStream(f),
ContentType.APPLICATION_OCTET_STREAM,
f.getName()
);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}); textBody.forEach((key, value) -> {
builder.addTextBody(String.valueOf(key), String.valueOf(value), ContentType.TEXT_PLAIN);
}); msg.setEntity(builder.build());
response = request.execute(msg);
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
entity = response.getEntity();
long length = entity.getContentLength();
byte[] res = null;
res = EntityUtils.toByteArray(entity);
result = new String(res, StandardCharsets.UTF_8);
} return result; } }
org.apache.httpcomponents.httpclient的更多相关文章
- httpclient工具使用(org.apache.httpcomponents.httpclient)
httpclient工具使用(org.apache.httpcomponents.httpclient) 引入依赖 <dependency> <groupId>org.apac ...
- org.apache.httpcomponents httpclient 发起HTTP JSON请求
1. pom.xml <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactI ...
- org.apache.httpcomponents:httpclient 工具类
基于httpclient 版本4.4.1 因为http连接需要三次握手,在需要频繁调用时浪费资源和时间 故采用连接池的方式连接 根据实际需要更改 连接池最大连接数.路由最大连接数 另一个需要注意的是 ...
- org.apache.commons.httpclient工具类
import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler; import org.apache.commons.httpcl ...
- JAVA中使用Apache HttpComponents Client的进行GET/POST请求使用案例
一.简述需求 平时我们需要在JAVA中进行GET.POST.PUT.DELETE等请求时,使用第三方jar包会比较简单.常用的工具包有: 1.https://github.com/kevinsawic ...
- org.apache.commons.httpclient工具类(封装的HttpUtil)
import java.io.BufferedInputStream; import java.io.File; import java.io.FileInputStream; import java ...
- Apache HttpComponents Client 4.0快速入门/升级-2.POST方法访问网页
Apache HttpComponents Client 4.0已经发布多时,httpclient项目从commons子项目挪到了HttpComponents子项目下,httpclient3.1和 h ...
- Apache HttpComponents中的cookie匹配策略
Apache HttpComponents中的cookie匹配策略 */--> pre.src {background-color: #292b2e; color: #b2b2b2;} pre. ...
- org.apache.commons.httpclient和org.apache.http.client区别(转)
官网说明: http://hc.apache.org/httpclient-3.x/ Commons HttpClient项目现已结束,不再开发.它已被其HttpClient和HttpCore模块中的 ...
随机推荐
- Django 上下文管理器,为父模板添加动态数据
1.摘要:模板继承可以减少页面内容的重复定义,实现页面内容的重用. 但是当父模板中有动态数据的话,这些动态数据在子模版中是不会显示的.我们可以通过自定义上下文处理器来解决 2.Django上下文处理器 ...
- [CodeIgniter4]讲解-加载静态页
讲解 本教程旨在向您介绍CodeIgniter框架和MVC体系结构的基本原理.它将向您展示如何以逐步的方式构造基本的CodeIgniter应用程序. 在本教程中,您将创建一个基本的新闻应用程序.您将从 ...
- vue制作滚动条幅-跑马灯效果实例代码
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- xmind修改默认配置
XMIND使用过程中,输入英文字符的时候,第1.2层级的英文字母总是默认大写,手动修改很繁琐.默认字体,想切换成其他类型,也是要手动一个个去修改. 网上找了下相关的问题,找到一些解决办法,整理到文档中 ...
- 打包Windowsform项目出现File 'Cognex.VisionPro3D.dll' targeting 'AMD64' is not compatible with the project's target platform 'x86'错误
错误信息: 个人理解此错误的大概意思是:打包的文件是64位的但是打包后的文件设置的是32位的,就出现冲突了. 解决方案:选择打包程序项目的属性窗口设置TargetPlatform属性为对应的值,本项目 ...
- 解决Office2016输入失效密钥之后无法输入的问题
第一步: 以管理员的身份运行cmd 第二步输入以下命令: cscript "C:\Program Files (x86)\Microsoft Office\Office16\OSPP.VBS ...
- window.resizeTo
概述 动态调整窗口的大小. 语法 window.resizeTo(aWidth, aHeight) 参数 aWidth 是一个整数,表示新的 outerWidth(单位:像素)(包括滚动条.窗口边框等 ...
- AntDesign(React)学习-5 路由及使用Layout布局
前言:学习目标实现点击登录按钮,直接进入后台布局页面,类似下面antd官网文档展示效果 ant.design访问 https://ant-design.gitee.io/components/menu ...
- SpringBoot--application.yml
application.properties 配置了端口号:9090 application.yml 也配置了端口号:8080 SpringBoot使用9090,以属性为主 1.在applicatio ...
- mybatis(三):框架结构