HttpClientUtil 工具类
- /*
- *
- *
- * FileName: s.java
- *
- * Description:TODO(用一句话描述该文件做什么)
- *
- * Created: jiangzhanghong 2017年11月14日
- *
- * |--------------------------------------------------History---------------------------------------------------|
- * | |
- * |-----Author-----------|-------Date-------|----Version----|----------------------------Desc----------------------------|
- * | jiangzhanghong | 2017年11月14日 | 1.0 | Create
- * |------------------------------------------------------------------------------------------------------------|
- */
- package com.dinfo.app.basic.util;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- /**
- *
- * @author <a href="mailto:jiangzhanghong1@ultrapower.com.cn">jiangzhanghong</a>
- * @version 1.0
- * @date 2017年11月14日
- */
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.OutputStream;
- import java.io.UnsupportedEncodingException;
- import java.net.HttpURLConnection;
- import java.net.MalformedURLException;
- import java.net.ProtocolException;
- import java.net.URL;
- import java.nio.charset.UnsupportedCharsetException;
- import java.security.KeyManagementException;
- import java.security.NoSuchAlgorithmException;
- import java.security.cert.CertificateException;
- import java.security.cert.X509Certificate;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- import javax.net.ssl.SSLContext;
- import javax.net.ssl.SSLException;
- import javax.net.ssl.SSLSession;
- import javax.net.ssl.SSLSocket;
- import javax.net.ssl.TrustManager;
- import javax.net.ssl.X509TrustManager;
- import org.apache.http.Consts;
- import org.apache.http.HttpEntity;
- import org.apache.http.HttpResponse;
- import org.apache.http.HttpStatus;
- import org.apache.http.NameValuePair;
- import org.apache.http.client.ClientProtocolException;
- import org.apache.http.client.config.RequestConfig;
- import org.apache.http.client.entity.UrlEncodedFormEntity;
- import org.apache.http.client.methods.CloseableHttpResponse;
- import org.apache.http.client.methods.HttpGet;
- import org.apache.http.client.methods.HttpPost;
- import org.apache.http.client.methods.HttpRequestBase;
- import org.apache.http.client.utils.URLEncodedUtils;
- import org.apache.http.conn.scheme.Scheme;
- import org.apache.http.conn.ssl.SSLSocketFactory;
- import org.apache.http.conn.ssl.X509HostnameVerifier;
- import org.apache.http.entity.ContentType;
- import org.apache.http.entity.StringEntity;
- import org.apache.http.entity.mime.HttpMultipartMode;
- 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.BasicHeader;
- import org.apache.http.message.BasicNameValuePair;
- import org.apache.http.protocol.HTTP;
- import org.apache.http.util.CharsetUtils;
- import org.apache.http.util.EntityUtils;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import org.springframework.beans.factory.annotation.Value;
- import org.springframework.stereotype.Component;
- /**
- * 封装了一些采用HttpClient发送HTTP请求的方法
- *
- * @see 本工具所采用的是最新的HttpComponents-Client-4.2.1
- */
- @Component
- public class HttpClientUtil {
- private HttpClientUtil() {
- }
- private static Logger logger = LoggerFactory.getLogger(HttpClientUtil.class);
- private static int connectTimeout=6000;
- private static int socketTimeout=60000;
- private static int connectionRequestTimeout=6000;
- public static void main(String[] args) {
- try {
- System.out.println(HttpClientUtil.sendGetRequest("http://baidu.com", null));
- } catch (Exception e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- /**
- * @param connectTimeout the connectTimeout to set
- */
- @Value("${httpclient.connectTimeout:6000}")
- public void setConnectTimeout(int connectTimeout) {
- HttpClientUtil.connectTimeout = connectTimeout;
- }
- /**
- * @param socketTimeout the socketTimeout to set
- */
- @Value("${httpclient.socketTimeout:60000}")
- public void setSocketTimeout(int socketTimeout) {
- HttpClientUtil.socketTimeout = socketTimeout;
- }
- /**
- * @param connectionRequestTimeout the connectionRequestTimeout to set
- */
- @Value("${httpclient.connectionRequestTimeout:6000}")
- public void setConnectionRequestTimeout(int connectionRequestTimeout) {
- HttpClientUtil.connectionRequestTimeout = connectionRequestTimeout;
- }
- /**
- * 发送HTTP_GET请求
- *
- * @see 该方法会自动关闭连接,释放资源
- * @param requestURL
- * 请求地址(含参数)
- * @param decodeCharset
- * 解码字符集,解析响应数据时用之,其为null时默认采用UTF-8解码
- * @return 远程主机响应正文
- * @throws IOException
- * @throws ClientProtocolException
- */
- public static String sendGetRequest(String reqURL, String decodeCharset) throws ClientProtocolException, IOException {
- long responseLength = 0; // 响应长度
- String responseContent = null; // 响应内容
- CloseableHttpClient httpClient = HttpClients.createDefault(); // 创建默认的httpClient实例
- HttpGet httpGet = new HttpGet(reqURL); // 创建org.apache.http.client.methods.HttpGet
- //设置超时
- setTimeout(httpGet);
- httpGet.setHeader(new BasicHeader("Content-Type", "application/x-www-form-urlencoded; charset=utf-8")); //解决get请求响应返回中文乱码问题
- try {
- HttpResponse response = httpClient.execute(httpGet); // 执行GET请求
- HttpEntity entity = response.getEntity(); // 获取响应实体
- if (null != entity) {
- responseLength = entity.getContentLength();
- responseContent = EntityUtils.toString(entity, decodeCharset == null ? "UTF-8" : decodeCharset);
- EntityUtils.consume(entity); // Consume response content
- }
- System.out.println("请求地址: " + httpGet.getURI());
- System.out.println("响应状态: " + response.getStatusLine());
- System.out.println("响应长度: " + responseLength);
- System.out.println("响应内容: " + responseContent);
- } finally {
- httpClient.close();
- }
- return responseContent;
- }
- /**
- * ConnectTimeout 连接建立时间,三次握手完成时间
- * SocketTimeout 数据传输过程中数据包之间间隔的最大时间
- * ConnectionRequestTimeout httpclient使用连接池来管理连接,这个时间就是从连接池获取连接的超时时间
- * @param base
- */
- private static void setTimeout(HttpRequestBase base){
- RequestConfig requestConfig = RequestConfig.custom()
- .setConnectTimeout(connectTimeout).setConnectionRequestTimeout(connectionRequestTimeout)
- .setSocketTimeout(socketTimeout).build();
- base.setConfig(requestConfig);
- }
- /**
- * 发送HTTP_POST请求
- *
- * @see 该方法为<code>sendPostRequest(String,String,boolean,String,String)</code>的简化方法
- * @see 该方法在对请求数据的编码和响应数据的解码时,所采用的字符集均为UTF-8
- * @see 当<code>isEncoder=true</code>时,其会自动对<code>sendData</code>中的[中文][|][
- * ]等特殊字符进行<code>URLEncoder.encode(string,"UTF-8")</code>
- * @param isEncoder
- * 用于指明请求数据是否需要UTF-8编码,true为需要
- * @throws IOException
- * @throws ClientProtocolException
- */
- public static String sendPostRequest(String reqURL, String sendData, boolean isEncoder) throws ClientProtocolException, IOException {
- return sendPostRequest(reqURL, sendData, isEncoder, null, null);
- }
- /**
- * 发送HTTP_POST请求
- *
- * @see 该方法会自动关闭连接,释放资源
- * @see 当<code>isEncoder=true</code>时,其会自动对<code>sendData</code>中的[中文][|][
- * ]等特殊字符进行<code>URLEncoder.encode(string,encodeCharset)</code>
- * @param reqURL
- * 请求地址
- * @param sendData
- * 请求参数,若有多个参数则应拼接成param11=value11&22=value22&33=value33的形式后,传入该参数中
- * @param isEncoder
- * 请求数据是否需要encodeCharset编码,true为需要
- * @param encodeCharset
- * 编码字符集,编码请求数据时用之,其为null时默认采用UTF-8解码
- * @param decodeCharset
- * 解码字符集,解析响应数据时用之,其为null时默认采用UTF-8解码
- * @return 远程主机响应正文
- * @throws IOException
- * @throws ClientProtocolException
- */
- public static String sendPostRequest(String reqURL, String sendData, boolean isEncoder, String encodeCharset,
- String decodeCharset) throws ClientProtocolException, IOException {
- String responseContent = null;
- CloseableHttpClient httpClient = HttpClients.createDefault();
- HttpPost httpPost = new HttpPost(reqURL);
- //设置超时
- setTimeout(httpPost);
- httpPost.setHeader(HTTP.CONTENT_TYPE, "application/x-www-form-urlencoded");
- try {
- if (isEncoder) {
- List<NameValuePair> formParams = new ArrayList<NameValuePair>();
- for (String str : sendData.split("&")) {
- formParams.add(new BasicNameValuePair(str.substring(0, str.indexOf("=")),
- str.substring(str.indexOf("=") + 1)));
- }
- httpPost.setEntity(new StringEntity(
- URLEncodedUtils.format(formParams, encodeCharset == null ? "UTF-8" : encodeCharset)));
- } else {
- httpPost.setEntity(new StringEntity(sendData));
- }
- HttpResponse response = httpClient.execute(httpPost);
- HttpEntity entity = response.getEntity();
- if (null != entity) {
- responseContent = EntityUtils.toString(entity, decodeCharset == null ? "UTF-8" : decodeCharset);
- EntityUtils.consume(entity);
- }
- } finally {
- httpClient.close();
- }
- return responseContent;
- }
- /**
- * 支持单个文件上传
- * @param reqURL 请求url
- * @param bytes 传递的二进制内容
- * @param fileparm 参数名称
- * @param decodeCharset
- * @return
- * @throws IOException
- * @throws ClientProtocolException
- * @throws Exception
- */
- public static String sendPostByte(String reqURL, byte[] bytes, String fileparm,String filename,
- String decodeCharset) throws ClientProtocolException, IOException {
- String responseContent = null;
- CloseableHttpClient httpClient = HttpClients.createDefault();
- HttpPost httpPost = new HttpPost(reqURL);
- //设置超时
- setTimeout(httpPost);
- MultipartEntityBuilder builder = MultipartEntityBuilder.create();
- builder.addBinaryBody(fileparm, bytes, ContentType.create("multipart/form-data","UTF-8"), filename);
- HttpEntity multipart = builder.build();
- httpPost.setEntity(multipart);
- try {
- HttpResponse response = httpClient.execute(httpPost);
- HttpEntity entity = response.getEntity();
- if (null != entity) {
- responseContent = EntityUtils.toString(entity, decodeCharset == null ? "UTF-8" : decodeCharset);
- EntityUtils.consume(entity);
- }
- } finally {
- httpClient.close();
- }
- return responseContent;
- }
- /**
- * 单个文件上传,file方式
- * @param reqURL
- * @param file
- * @param fileparm
- * @param decodeCharset
- * @return
- * @throws UnsupportedCharsetException
- * @throws IOException
- * @throws ClientProtocolException
- * @throws Exception
- */
- public static String sendPostFile(String reqURL,File file, String fileparm,
- String decodeCharset) throws UnsupportedCharsetException, ClientProtocolException, IOException {
- String responseContent = null;
- CloseableHttpClient httpClient = HttpClients.createDefault();
- HttpPost httpPost = new HttpPost(reqURL);
- //设置超时
- setTimeout(httpPost);
- MultipartEntityBuilder builder = MultipartEntityBuilder.create();
- builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
- builder.setCharset(CharsetUtils.get("UTF-8")); //设置编码,解决上传文件名乱码问题
- builder.addBinaryBody(fileparm, new FileInputStream(file), ContentType.create("multipart/form-data","UTF-8"), file.getName());
- HttpEntity multipart = builder.build();
- httpPost.setEntity(multipart);
- try {
- HttpResponse response = httpClient.execute(httpPost);
- HttpEntity entity = response.getEntity();
- if (null != entity) {
- responseContent = EntityUtils.toString(entity, decodeCharset == null ? "UTF-8" : decodeCharset);
- EntityUtils.consume(entity);
- }
- } finally {
- httpClient.close();
- }
- return responseContent;
- }
- public static String sendPostMutipart(String reqURL, List<byte[]> bytes, List<String> fileparams,List<String> fileNames,List<String> paramNames,List<String> paramValues,
- String decodeCharset) throws ClientProtocolException, IOException {
- String responseContent = null;
- CloseableHttpClient httpClient = HttpClients.createDefault();
- HttpPost httpPost = new HttpPost(reqURL);
- //设置超时
- setTimeout(httpPost);
- MultipartEntityBuilder builder = MultipartEntityBuilder.create();
- builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
- builder.setCharset(CharsetUtils.get("UTF-8"));
- for (int i = 0; i < bytes.size(); i++) {
- builder.addBinaryBody(fileparams.get(i), bytes.get(i), ContentType.create("multipart/form-data","UTF-8"), fileNames.get(i));
- }
- for (int i = 0; i < paramNames.size(); i++) {
- builder.addTextBody(paramNames.get(i), paramValues.get(i), ContentType.create("multipart/form-data","UTF-8"));
- }
- HttpEntity multipart = builder.build();
- httpPost.setEntity(multipart);
- try {
- HttpResponse response = httpClient.execute(httpPost);
- HttpEntity entity = response.getEntity();
- if (null != entity) {
- responseContent = EntityUtils.toString(entity, decodeCharset == null ? "UTF-8" : decodeCharset);
- EntityUtils.consume(entity);
- }
- } finally {
- httpClient.close();
- }
- return responseContent;
- }
- /**
- * 发送HTTP_POST请求
- *
- * @see 该方法会自动关闭连接,释放资源
- * @see 该方法会自动对<code>params</code>中的[中文][|][
- * ]等特殊字符进行<code>URLEncoder.encode(string,encodeCharset)</code>
- * @param reqURL
- * 请求地址
- * @param params
- * 请求参数
- * @param encodeCharset
- * 编码字符集,编码请求数据时用之,其为null时默认采用UTF-8解码
- * @param decodeCharset
- * 解码字符集,解析响应数据时用之,其为null时默认采用UTF-8解码
- * @return 远程主机响应正文
- * @throws IOException
- * @throws ClientProtocolException
- */
- public static String sendPostRequest(String reqURL, Map<String, String> params, String encodeCharset,
- String decodeCharset) throws ClientProtocolException, IOException {
- String responseContent = null;
- CloseableHttpClient httpClient = HttpClients.createDefault();
- if(params==null){
- params=new HashMap<String, String>();
- }
- HttpPost httpPost = new HttpPost(reqURL);
- //设置超时
- //setTimeout(httpPost);
- List<NameValuePair> formParams = new ArrayList<NameValuePair>(); // 创建参数队列
- for (Map.Entry<String, String> entry : params.entrySet()) {
- formParams.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
- }
- try {
- httpPost.setEntity(new UrlEncodedFormEntity(formParams, encodeCharset == null ? "UTF-8" : encodeCharset));
- HttpResponse response = httpClient.execute(httpPost);
- HttpEntity entity = response.getEntity();
- if (null != entity) {
- responseContent = EntityUtils.toString(entity, decodeCharset == null ? "UTF-8" : decodeCharset);
- EntityUtils.consume(entity);
- }
- } finally {
- httpClient.close();
- }
- return responseContent;
- }
- /**
- * 发送HTTPS_POST请求
- * @throws IOException
- * @throws ClientProtocolException
- * @throws NoSuchAlgorithmException
- * @throws KeyManagementException
- *
- * @see 该方法为<code>sendPostSSLRequest(String,Map<String,String>,String,String)</code>方法的简化方法
- * @see 该方法在对请求数据的编码和响应数据的解码时,所采用的字符集均为UTF-8
- * @see 该方法会自动对<code>params</code>中的[中文][|][
- * ]等特殊字符进行<code>URLEncoder.encode(string,"UTF-8")</code>
- */
- public static String sendPostSSLRequest(String reqURL, Map<String, String> params) throws KeyManagementException, NoSuchAlgorithmException, ClientProtocolException, IOException {
- return sendPostSSLRequest(reqURL, params, null, null);
- }
- /**
- * 发送HTTPS_POST请求
- *
- * @see 该方法会自动关闭连接,释放资源
- * @see 该方法会自动对<code>params</code>中的[中文][|][
- * ]等特殊字符进行<code>URLEncoder.encode(string,encodeCharset)</code>
- * @param reqURL
- * 请求地址
- * @param params
- * 请求参数
- * @param encodeCharset
- * 编码字符集,编码请求数据时用之,其为null时默认采用UTF-8解码
- * @param decodeCharset
- * 解码字符集,解析响应数据时用之,其为null时默认采用UTF-8解码
- * @return 远程主机响应正文
- * @throws NoSuchAlgorithmException
- * @throws KeyManagementException
- * @throws IOException
- * @throws ClientProtocolException
- */
- public static String sendPostSSLRequest(String reqURL, Map<String, String> params, String encodeCharset,
- String decodeCharset) throws NoSuchAlgorithmException, KeyManagementException, ClientProtocolException, IOException {
- String responseContent = "";
- CloseableHttpClient httpClient = HttpClients.createDefault();
- X509TrustManager xtm = new X509TrustManager() {
- public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
- }
- public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
- }
- public X509Certificate[] getAcceptedIssuers() {
- return null;
- }
- };
- try {
- SSLContext ctx = SSLContext.getInstance("TLS");
- ctx.init(null, new TrustManager[] { xtm }, null);
- SSLSocketFactory socketFactory = new SSLSocketFactory(ctx);
- httpClient.getConnectionManager().getSchemeRegistry().register(new Scheme("https", 443, socketFactory));
- HttpPost httpPost = new HttpPost(reqURL);
- //设置超时
- setTimeout(httpPost);
- List<NameValuePair> formParams = new ArrayList<NameValuePair>();
- for (Map.Entry<String, String> entry : params.entrySet()) {
- formParams.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
- }
- httpPost.setEntity(new UrlEncodedFormEntity(formParams, encodeCharset == null ? "UTF-8" : encodeCharset));
- HttpResponse response = httpClient.execute(httpPost);
- HttpEntity entity = response.getEntity();
- if (null != entity) {
- responseContent = EntityUtils.toString(entity, decodeCharset == null ? "UTF-8" : decodeCharset);
- EntityUtils.consume(entity);
- }
- } finally {
- httpClient.close();
- }
- return responseContent;
- }
- /**
- * 发送HTTP_POST请求
- *
- * @see 若发送的<code>params</code>中含有中文,记得按照双方约定的字符集将中文<code>URLEncoder.encode(string,encodeCharset)</code>
- * @see 本方法默认的连接超时时间为30秒,默认的读取超时时间为30秒
- * @param reqURL
- * 请求地址
- * @param params
- * 发送到远程主机的正文数据,其数据类型为<code>java.util.Map<String, String></code>
- * @return 远程主机响应正文`HTTP状态码,如<code>"SUCCESS`200"</code><br>
- * 若通信过程中发生异常则返回"Failed`HTTP状态码",如<code>"Failed`500"</code>
- * @throws IOException
- * @throws Exception
- */
- public static String sendPostRequestByJava(String reqURL, Map<String, String> params) throws IOException {
- StringBuilder sendData = new StringBuilder();
- for (Map.Entry<String, String> entry : params.entrySet()) {
- sendData.append(entry.getKey()).append("=").append(entry.getValue()).append("&");
- }
- if (sendData.length() > 0) {
- sendData.setLength(sendData.length() - 1); // 删除最后一个&符号
- }
- return sendPostRequestByJava(reqURL, sendData.toString());
- }
- /**
- * 发送HTTP_POST请求
- *
- * @see 若发送的<code>sendData</code>中含有中文,记得按照双方约定的字符集将中文<code>URLEncoder.encode(string,encodeCharset)</code>
- * @see 本方法默认的连接超时时间为30秒,默认的读取超时时间为30秒
- * @param reqURL
- * 请求地址
- * @param sendData
- * 发送到远程主机的正文数据
- * @return 远程主机响应正文`HTTP状态码,如<code>"SUCCESS`200"</code><br>
- * 若通信过程中发生异常则返回"Failed`HTTP状态码",如<code>"Failed`500"</code>
- * @throws IOException
- */
- public static String sendPostRequestByJava(String reqURL, String sendData) throws IOException {
- HttpURLConnection httpURLConnection = null;
- OutputStream out = null; // 写
- InputStream in = null; // 读
- int httpStatusCode = 0; // 远程主机响应的HTTP状态码
- try {
- URL sendUrl = new URL(reqURL);
- httpURLConnection = (HttpURLConnection) sendUrl.openConnection();
- httpURLConnection.setRequestMethod("POST");
- httpURLConnection.setDoOutput(true); // 指示应用程序要将数据写入URL连接,其值默认为false
- httpURLConnection.setUseCaches(false);
- httpURLConnection.setConnectTimeout(60000); // 60秒连接超时
- httpURLConnection.setReadTimeout(60000); // 60秒读取超时
- out = httpURLConnection.getOutputStream();
- out.write(sendData.toString().getBytes());
- // 清空缓冲区,发送数据
- out.flush();
- // 获取HTTP状态码
- httpStatusCode = httpURLConnection.getResponseCode();
- in = httpURLConnection.getInputStream();
- byte[] byteDatas = new byte[in.available()];
- in.read(byteDatas);
- return new String(byteDatas) + "`" + httpStatusCode;
- } finally {
- if (out != null) {
- try {
- out.close();
- } catch (Exception e) {
- logger.debug("关闭输出流时发生异常,堆栈信息如下", e);
- }
- }
- if (in != null) {
- try {
- in.close();
- } catch (Exception e) {
- logger.debug("关闭输入流时发生异常,堆栈信息如下", e);
- }
- }
- if (httpURLConnection != null) {
- httpURLConnection.disconnect();
- httpURLConnection = null;
- }
- }
- }
- /**
- * https posp请求,可以绕过证书校验
- *
- * @param url
- * @param params
- * @return
- * @throws NoSuchAlgorithmException
- * @throws KeyManagementException
- * @throws IOException
- * @throws ClientProtocolException
- */
- public static final String sendHttpsRequestByPost(String url, Map<String, String> params) throws NoSuchAlgorithmException, KeyManagementException, ClientProtocolException, IOException {
- String responseContent = null;
- CloseableHttpClient httpClient = HttpClients.createDefault();
- // 创建TrustManager
- X509TrustManager xtm = new X509TrustManager() {
- public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
- }
- public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
- }
- public X509Certificate[] getAcceptedIssuers() {
- return null;
- }
- };
- // 这个好像是HOST验证
- X509HostnameVerifier hostnameVerifier = new X509HostnameVerifier() {
- public boolean verify(String arg0, SSLSession arg1) {
- return true;
- }
- public void verify(String arg0, SSLSocket arg1) throws IOException {
- }
- public void verify(String arg0, String[] arg1, String[] arg2) throws SSLException {
- }
- public void verify(String arg0, X509Certificate arg1) throws SSLException {
- }
- };
- try {
- // TLS1.0与SSL3.0基本上没有太大的差别,可粗略理解为TLS是SSL的继承者,但它们使用的是相同的SSLContext
- SSLContext ctx = SSLContext.getInstance("TLS");
- // 使用TrustManager来初始化该上下文,TrustManager只是被SSL的Socket所使用
- ctx.init(null, new TrustManager[] { xtm }, null);
- // 创建SSLSocketFactory
- SSLSocketFactory socketFactory = new SSLSocketFactory(ctx);
- socketFactory.setHostnameVerifier(hostnameVerifier);
- // 通过SchemeRegistry将SSLSocketFactory注册到我们的HttpClient上
- httpClient.getConnectionManager().getSchemeRegistry().register(new Scheme("https", socketFactory, 443));
- HttpPost httpPost = new HttpPost(url);
- //设置超时
- setTimeout(httpPost);
- List<NameValuePair> formParams = new ArrayList<NameValuePair>(); // 构建POST请求的表单参数
- for (Map.Entry<String, String> entry : params.entrySet()) {
- formParams.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
- }
- httpPost.setEntity(new UrlEncodedFormEntity(formParams, "UTF-8"));
- HttpResponse response = httpClient.execute(httpPost);
- HttpEntity entity = response.getEntity(); // 获取响应实体
- if (entity != null) {
- responseContent = EntityUtils.toString(entity, "UTF-8");
- }
- } finally {
- // 关闭连接,释放资源
- try {
- httpClient.close();
- } catch (IOException e) {
- logger.error(e.getMessage());
- }
- }
- return responseContent;
- }
- /**
- * 发送HTTP_POST请求,json格式数据
- *
- * @param url
- * @param body
- * @return
- * @throws IOException
- * @throws ClientProtocolException
- * @throws Exception
- */
- public static String sendPostByJson(String url, String body) throws ClientProtocolException, IOException {
- CloseableHttpClient httpclient = HttpClients.custom().build();
- HttpPost post = null;
- String resData = null;
- CloseableHttpResponse result = null;
- try {
- post = new HttpPost(url);
- //设置超时
- setTimeout(post);
- HttpEntity entity2 = new StringEntity(body, Consts.UTF_8);
- post.setConfig(RequestConfig.custom().setConnectTimeout(connectTimeout).setSocketTimeout(socketTimeout).build());
- post.setHeader("Content-Type", "application/json");
- post.setEntity(entity2);
- result = httpclient.execute(post);
- if (HttpStatus.SC_OK == result.getStatusLine().getStatusCode()) {
- resData = EntityUtils.toString(result.getEntity());
- }
- } finally {
- if (result != null) {
- result.close();
- }
- if (post != null) {
- post.releaseConnection();
- }
- httpclient.close();
- }
- return resData;
- }
- }
HttpClientUtil 工具类的更多相关文章
- HttpClientUtil工具类,待更新
package com.igs.webShop.web.util; import org.apache.http.HttpEntity;import org.apache.http.HttpRespo ...
- 通用HttpClientUtil工具类
package com.*.utils; import java.io.IOException; import java.net.URI; import java.util.ArrayList; im ...
- HttpClientUtil 工具类 实现跨域请求数据
package com.xxx.common.util; import java.io.IOException; import java.net.URI; import java.util.Array ...
- HttpClientUtil工具类封装
package com.jd.ng.shiro.utils; import org.apache.http.HttpEntity; import org.apache.http.HttpStatus; ...
- 轻松把玩HttpClient之封装HttpClient工具类(五),携带Cookie的请求
近期更新了一下HttpClientUtil工具类代码,主要是加入了一个參数HttpContext,这个是用来干嘛的呢?事实上是用来保存和传递Cookie所须要的. 由于我们有非常多时候都须要登录.然后 ...
- 基于HttpClient4.5.2实现的HttpClient工具类
1.maven依赖: <dependency> <groupId>org.apache.commons</groupId> <artifactId>co ...
- 带有连接池的Http客户端工具类HttpClientUtil
一.背景 业务开发中,经常会遇到通过http/https向下游服务发送请求.每次都要重复造轮子写HttpClient的逻辑,而且性能.功能参差不齐.这里分享一个高性能的.带连接池的通用Http客户端工 ...
- HttpClient 4.5.x 工具类设计与实现
最近,业务需要在java服务端发起http请求,需要实现"GET","POST","PUT"等基本方法.于是想以 "HttpCli ...
- Java开发小技巧(五):HttpClient工具类
前言 大多数Java应用程序都会通过HTTP协议来调用接口访问各种网络资源,JDK也提供了相应的HTTP工具包,但是使用起来不够方便灵活,所以我们可以利用Apache的HttpClient来封装一个具 ...
随机推荐
- TFS撤销其他人的迁出
1.cd C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE 2.查看工作区tf workspaces /owner:zho ...
- [原]System.IO.Path.Combine 路径合并
使用 ILSpy 工具查看了 System.IO.Path 类中的 Combine 方法 对它的功能有点不放心,原方法实现如下: // System.IO.Path /// <summary&g ...
- 3-4 1449 web view
1.app类型 不同类型的应用 区别 native app 纯原声app,Android用Java些,iOS用object c写 hybrid app 套用原声应用的外壳,既有原生的UI页面,又通过内 ...
- js用法
属性(attribute) function fn(){ console.log(123) } fn() var a=fn() 将函数fn()调用结果赋值给a 1.函数 ...
- docker 基础操作
1. 安装docker 系统centos 7.2 yum -y install docker-io service docker start 安装完毕后执行 docker version 或者dock ...
- Linux下安装Nginx依赖包和Nginx的命令
1.安装依赖包pcrecd /usr/local/srcwget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.39.tar ...
- Docker集群管理(一)—— 基础docker+swarm+shipyard
目的 学习docker的集群管理,摸索出高可用的docker微服务架构方案.本篇文章只初步的了解下swarm(docker新版已集成了swarm)的使用,了解docker的发现服务的基础方法(dock ...
- Django中间件执行流程
中间件函数是 django 框架为我们预留的函数接口, 让我们可以干预请求和应答的过程 1. 获取浏览器端的IP地址: 使用 request.META[‘REMOTE_ADDR’] 2. 使用中间件 ...
- IO流程及优化
http://blog.csdn.net/xypzwl/article/details/51416883 一.存储设备的存储原理 机械硬盘: 机械硬盘使用磁性物质作为存储介质,用N.S极性来代表0或1 ...
- solr入门之搜索建议的几种实现方式和最终选取实现思路
上篇博客中我简单的讲了下solr自身的suggest模块来实现搜索建议.但是今天研究了下在solr自身的suggest中添加进去拼音来智能推荐时不时很方便.在次从网上搜集和整理思考了下该问题的解决. ...