java http 表单提交_JAVA后台使用HTTPCLIENT入门HTTPPOST请求(FORM表单提交,FILE文件上传和传输JSON数据)...

一、HttpClient 简介

HttpClient 是 Apache Jakarta Common 下的子项目,用来提供高效的、最新的、功能丰富的支持 HTTP 协议的客户端编程工具包,并且它支持 HTTP 协议最新的版本和建议。Java后台使用httpclient主要目的是为了模拟客户端的请求。

2、HttpClient的请求类型

实现了所有的Http请求类型,相应的类为:HttpGet、HttpPost、HttpDelete、HttpPut

3、Http的使用流程

1)导包

org.apache.httpcomponents

httpclient

4.5.5

org.apache.httpcomponents

httpmime

4.5

2)创建连接所需要的配置信息

public class HttpClientConfig {

static int httpConnectTimeout = 10000;//连接超时时间(单位毫秒)

static int httpSocketTimeout = 10000;//socket读写超时时间(单位毫秒)

static int httpMaxPoolSize = 100;

static int httpMonitorInterval = 3000;

static int httpIdelTimeout = 2000;

public static int getHttpIdelTimeout() {

return httpIdelTimeout;

}

public static int getHttpSocketTimeout() {

return httpSocketTimeout;

}

public static int getHttpMaxPoolSize() {

return httpMaxPoolSize;

}

public static int getHttpMonitorInterval() {

return httpMonitorInterval;

}

public static int getHttpConnectTimeout() {

return httpConnectTimeout;

}

}

3)封装HttpClientUtils类--包括连接池的信息

/**

*导包

*/

importcom.scxd.config.HttpClientConfig;import org.apache.http.*;importorg.apache.http.client.HttpRequestRetryHandler;importorg.apache.http.client.config.RequestConfig;importorg.apache.http.client.methods.CloseableHttpResponse;importorg.apache.http.client.methods.HttpPost;importorg.apache.http.client.methods.HttpRequestBase;importorg.apache.http.client.protocol.HttpClientContext;importorg.apache.http.config.Registry;importorg.apache.http.config.RegistryBuilder;importorg.apache.http.conn.ConnectTimeoutException;importorg.apache.http.conn.routing.HttpRoute;importorg.apache.http.conn.socket.ConnectionSocketFactory;importorg.apache.http.conn.socket.LayeredConnectionSocketFactory;importorg.apache.http.conn.socket.PlainConnectionSocketFactory;importorg.apache.http.conn.ssl.SSLConnectionSocketFactory;importorg.apache.http.entity.ContentType;importorg.apache.http.entity.mime.MultipartEntityBuilder;importorg.apache.http.entity.mime.content.StringBody;importorg.apache.http.impl.client.CloseableHttpClient;importorg.apache.http.impl.client.HttpClients;importorg.apache.http.impl.conn.PoolingHttpClientConnectionManager;importorg.apache.http.protocol.HttpContext;importorg.apache.http.util.EntityUtils;importjavax.net.ssl.SSLException;importjavax.net.ssl.SSLHandshakeException;importjava.io.IOException;importjava.io.InterruptedIOException;importjava.net.UnknownHostException;importjava.util.Map;importjava.util.TimerTask;importjava.util.concurrent.Executors;importjava.util.concurrent.ScheduledExecutorService;importjava.util.concurrent.TimeUnit;import org.apache.log4j.Logger;

public class HttpClientUtils {

private final static Logger logger = Logger.getLogger(HttpClientUtils.class);

private static CloseableHttpClient httpClient;

private static PoolingHttpClientConnectionManager manager; // 连接池管理类

private static ScheduledExecutorService monitorExecutor; // 监控

private final static Object syncLock = new Object(); // 相当于线程锁,用于线程安全

private static final int CONNECT_TIMEOUT = HttpClientConfig.getHttpConnectTimeout();// 设置连接建立的超时时间为10s

private static final int SOCKET_TIMEOUT = HttpClientConfig.getHttpSocketTimeout();

private static final int MAX_CONN = HttpClientConfig.getHttpMaxPoolSize(); // 最大连接数

private static final int Max_PRE_ROUTE = HttpClientConfig.getHttpMaxPoolSize();

private static final int MAX_ROUTE = HttpClientConfig.getHttpMaxPoolSize();

/**

* 对http请求进行基本设置

*

* @param httpRequestBase

* http请求

*/

private static void setRequestConfig(HttpRequestBase httpRequestBase) {

RequestConfig requestConfig = RequestConfig.custom().setConnectionRequestTimeout(CONNECT_TIMEOUT)

.setConnectTimeout(CONNECT_TIMEOUT).setSocketTimeout(SOCKET_TIMEOUT).build();

httpRequestBase.setConfig(requestConfig);

}

public static CloseableHttpClient getHttpClient(String url) {

String hostName = url.split("/")[2];

// System.out.println(hostName);

int port = 80;

if (hostName.contains(":")) {

String[] args = hostName.split(":");

hostName = args[0];

port = Integer.parseInt(args[1]);

}

if (httpClient == null) {

// 多线程下多个线程同时调用getHttpClient容易导致重复创建httpClient对象的问题,所以加上了同步锁

synchronized (syncLock) {

if (httpClient == null) {

httpClient = createHttpClient(hostName, port);

// 开启监控线程,对异常和空闲线程进行关闭

monitorExecutor = Executors.newScheduledThreadPool(1);

monitorExecutor.scheduleAtFixedRate(new TimerTask() {

@Override

public void run() {

// 关闭异常连接

manager.closeExpiredConnections();

// 关闭5s空闲的连接

manager.closeIdleConnections(HttpClientConfig.getHttpIdelTimeout(), TimeUnit.MILLISECONDS);

logger.debug("close expired and idle for over 5s connection");

}

}, HttpClientConfig.getHttpMonitorInterval(), HttpClientConfig.getHttpMonitorInterval(),

TimeUnit.MILLISECONDS);

}

}

}

return httpClient;

}

/**

* 根据host和port构建httpclient实例

*

* @param host

* 要访问的域名

* @param port

* 要访问的端口

* @return

*/

public static CloseableHttpClient createHttpClient(String host, int port) {

ConnectionSocketFactory plainSocketFactory = PlainConnectionSocketFactory.getSocketFactory();

LayeredConnectionSocketFactory sslSocketFactory = SSLConnectionSocketFactory.getSocketFactory();

Registry registry = RegistryBuilder. create()

.register("http", plainSocketFactory).register("https", sslSocketFactory).build();

manager = new PoolingHttpClientConnectionManager(registry);

// 设置连接参数

manager.setMaxTotal(MAX_CONN); // 最大连接数

manager.setDefaultMaxPerRoute(Max_PRE_ROUTE); // 路由最大连接数

HttpHost httpHost = new HttpHost(host, port);

manager.setMaxPerRoute(new HttpRoute(httpHost), MAX_ROUTE);

// 请求失败时,进行请求重试

HttpRequestRetryHandler handler = new HttpRequestRetryHandler() {

@Override

public boolean retryRequest(IOException e, int i, HttpContext httpContext) {

if (i > 3) {

// 重试超过3次,放弃请求

logger.error("retry has more than 3 time, give up request");

return false;

}

if (e instanceof NoHttpResponseException) {

// 服务器没有响应,可能是服务器断开了连接,应该重试

logger.error("receive no response from server, retry");

return true;

}

if (e instanceof SSLHandshakeException) {

// SSL握手异常

logger.error("SSL hand shake exception");

return false;

}

if (e instanceof InterruptedIOException) {

// 超时

logger.error("InterruptedIOException");

return false;

}

if (e instanceof UnknownHostException) {

// 服务器不可达

logger.error("server host unknown");

return false;

}

if (e instanceof ConnectTimeoutException) {

// 连接超时

logger.error("Connection Time out");

return false;

}

if (e instanceof SSLException) {

logger.error("SSLException");

return false;

}

HttpClientContext context = HttpClientContext.adapt(httpContext);

HttpRequest request = context.getRequest();

if (!(request instanceof HttpEntityEnclosingRequest)) {

// 如果请求不是关闭连接的请求

return true;

}

return false;

}

};

CloseableHttpClient client = HttpClients.custom().setConnectionManager(manager).setRetryHandler(handler)

.build();

return client;

}

/**

* 关闭连接池

*/

public static void closeConnectionPool() {

try {

httpClient.close();

manager.close();

monitorExecutor.shutdown();

} catch (IOException e) {

e.printStackTrace();

}

}

}

/**

* 对http请求进行基本设置

*

* @param httpRequestBase

* http请求

*/

private static void setRequestConfig(HttpRequestBase httpRequestBase) {

RequestConfig requestConfig = RequestConfig.custom().setConnectionRequestTimeout(CONNECT_TIMEOUT)

.setConnectTimeout(CONNECT_TIMEOUT).setSocketTimeout(SOCKET_TIMEOUT).build();

httpRequestBase.setConfig(requestConfig);

}

4)form表单提交

public static String doPostForm(String url, Map params) {

HttpPost httpPost = new HttpPost(url);

setRequestConfig(httpPost);

String resultString = "";

CloseableHttpResponse response = null;

try {

MultipartEntityBuilder builder = MultipartEntityBuilder.create();

if (params != null) {

for (String key : params.keySet()) {

builder.addPart(key,

new StringBody(params.get(key), ContentType.create("text/plain", Consts.UTF_8)));

}

}

HttpEntity reqEntity = builder.build();

httpPost.setEntity(reqEntity);

// 发起请求 并返回请求的响应

response = getHttpClient(url).execute(httpPost, HttpClientContext.create());

resultString = EntityUtils.toString(response.getEntity(), "utf-8");

} catch (IOException e) {

e.printStackTrace();

} catch (Exception e) {

e.printStackTrace();

} finally {

try {

if (response != null)

response.close();

} catch (IOException e) {

e.printStackTrace();

}

}

return resultString;

}

5)File文件上传

public static String uploadFile(String url, String localFile, String fileParamName, Map params) {

HttpPost httpPost = new HttpPost(url);

setRequestConfig(httpPost);

String resultString = "";

CloseableHttpResponse response = null;

try {

// 把文件转换成流对象FileBody

FileBody bin = new FileBody(new File(localFile));

MultipartEntityBuilder builder = MultipartEntityBuilder.create();

// 相当于

builder.addPart("files", bin);

// 相当于

builder.addPart("filesFileName",

new StringBody(fileParamName, ContentType.create("text/plain", Consts.UTF_8)));

if (params != null) {

for (String key : params.keySet()) {

builder.addPart(key,

new StringBody(params.get(key), ContentType.create("text/plain", Consts.UTF_8)));

}

}

HttpEntity reqEntity = builder.build();

httpPost.setEntity(reqEntity);

// 发起请求 并返回请求的响应

response = getHttpClient(url).execute(httpPost, HttpClientContext.create());

resultString = EntityUtils.toString(response.getEntity(), "utf-8");

} catch (IOException e) {

e.printStackTrace();

} catch (Exception e) {

e.printStackTrace();

} finally {

try {

if (response != null)

response.close();

} catch (IOException e) {

e.printStackTrace();

}

}

return resultString;

}

6) 传输Json数据

public static String doPostJson(String url, String json) {

HttpPost httpPost = new HttpPost(url);

setRequestConfig(httpPost);

String resultString = "";

CloseableHttpResponse response = null;

try {

// 设置ContentType(注:如果只是传普通参数的话,ContentType不一定非要用application/json)

// httpPost.setHeader("Content-Type",

// "application/json;charset=utf8");

httpPost.setHeader("Content-Type", "application/json");

// 创建请求内容

StringEntity entity = new StringEntity(json, ContentType.APPLICATION_JSON);

httpPost.setEntity(entity);

// 执行http请求

response = getHttpClient(url).execute(httpPost, HttpClientContext.create());

resultString = EntityUtils.toString(response.getEntity(), "utf-8");

} catch (Exception e) {

logger.error("httpclient的get请求失败,url:" + url, e);

// e.printStackTrace();

} finally {

try {

if (response != null)

response.close();

} catch (IOException e) {

logger.error("IOException的错误", e);

// e.printStackTrace();

}

}

return resultString;

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值