HTTPS 客户端发送请求(四)

本文介绍了如何在HTTP和HTTPS请求之间灵活转换,包括修改请求的port和设置无证书请求方式,通过使用Apache HttpClient库实现了HTTP、GET、PUT、DELETE和POST请求的发送,并处理了重定向情况。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

HTTP与HTTPS灵活转换请求



前言:在工作中我们可能不一定只用到https和http,有时候可能两者都会用到,那我们发送了一个连接的时候呢,我们如何灵活的转换两者的请求呢,此时我们的一个思路就是,首先是将请求的port修改,比如讲http://修改为了https://,第二部就是要讲https设置为无证书请求方式,这个地方我们用到了apache给我们提供的类进行转换,很好用的哦~~~


Maven管理


	<!-- httpclient begin -->
		<dependency>
			<groupId>org.apache.httpcomponents</groupId>
			<artifactId>httpclient</artifactId>
			<version>4.3.4</version>
		</dependency>
		<dependency>
			<groupId>org.apache.httpcomponents</groupId>
			<artifactId>httpasyncclient</artifactId>
			<version>4.0.2</version>
		</dependency>
		<dependency>
			<groupId>commons-httpclient</groupId>
			<artifactId>commons-httpclient</artifactId>
			<version>3.1</version>
		</dependency>
		<!-- httpclient end -->


转换类实现


package com.lives.platform.common.util;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
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.List;
import java.util.Map;

import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;

import org.apache.commons.httpclient.HttpStatus;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
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.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.config.Registry;
import org.apache.http.config.RegistryBuilder;
import org.apache.http.conn.socket.ConnectionSocketFactory;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.BasicHttpClientConnectionManager;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.apache.log4j.Logger;

public class HttpClientUtils {

	private static final String HTTPS_PROTOCOL = "https://";
	private static final int HTTPS_PROTOCOL_DEFAULT_PORT = 443;

	/**
	 * 默认编码格式
	 */
	private static final String DEFAULT_CHARSET = "UTF-8";

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

	private static int getPort(String url) {
		int startIndex = url.indexOf("://") + "://".length();
		String host = url.substring(startIndex);
		if (host.indexOf("/") != -1) {
			host = host.substring(0, host.indexOf("/"));
		}
		int port = HTTPS_PROTOCOL_DEFAULT_PORT;
		if (host.contains(":")) {
			int i = host.indexOf(":");
			port = new Integer(host.substring(i + 1));
		}
		return port;
	}

	private static List<NameValuePair> geneNameValPairs(
			Map<String, ?> params) {
		List<NameValuePair> pairs = new ArrayList<NameValuePair>();
		if (params == null) {
			return pairs;
		}
		for (String name : params.keySet()) {
			if (params.get(name) == null) {
				continue;
			}
			pairs.add(new BasicNameValuePair(name, params.get(name).toString()));
		}
		return pairs;
	}

	/**
	 * 发送GET请求
	 * @param url 请求地址
	 * @param charset 返回数据编码
	 * @return 返回数据
	 */
	public static String sendGetReq(final String url, String charset) {
		if (null == charset)
			charset = DEFAULT_CHARSET;
		CloseableHttpClient httpClient = HttpClientBuilder.create().build();
		HttpGet get = new HttpGet(url);
		if (url.toLowerCase().startsWith(HTTPS_PROTOCOL)) {
			initSSL(httpClient, getPort(url));
		}
		try {
			// 提交请求并以指定编码获取返回数据
			HttpResponse httpResponse = httpClient.execute(get);
			int statuscode = httpResponse.getStatusLine().getStatusCode();
	        if ((statuscode == HttpStatus.SC_MOVED_TEMPORARILY) ||
	            (statuscode == HttpStatus.SC_MOVED_PERMANENTLY) ||
	            (statuscode == HttpStatus.SC_SEE_OTHER) ||
	            (statuscode == HttpStatus.SC_TEMPORARY_REDIRECT)) {
	        	Header header = httpResponse.getFirstHeader("location");
	            if (header != null) {
	                String newuri = header.getValue();
	                if ((newuri == null) || (newuri.equals("")))
	                    newuri = "/";
	                try {
	    				httpClient.close();
	    			} catch (Exception e) {
	    				e.printStackTrace();
	    				httpClient = null;
	    			}
	                logger.info("重定向地址:" + newuri);
	                return sendGetReq(newuri, null);
	            }
	        }
			logger.info("请求地址:" + url + ";响应状态:" + httpResponse.getStatusLine());
			HttpEntity entity = httpResponse.getEntity();
			return EntityUtils.toString(entity, charset);
		} catch (ClientProtocolException e) {
			logger.error("协议异常,堆栈信息如下", e);
		} catch (IOException e) {
			logger.error("网络异常,堆栈信息如下", e);
		} finally {
			// 关闭连接,释放资源
			try {
				httpClient.close();
			} catch (Exception e) {
				e.printStackTrace();
				httpClient = null;
			}
		}
		return null;
	}

	/**
	 * 发送put请求
	 * @param url		请求地址
	 * @param params	请求参数
	 * @param charset	返回数据编码
	 * @return
	 */
	public static String sendPutReq(String url, Map<String, Object> params, String charset) {
		if (null == charset)
			charset = DEFAULT_CHARSET;
		CloseableHttpClient httpClient = HttpClientBuilder.create().build();
		HttpPut put = new HttpPut(url);

		// 封装请求参数
		List<NameValuePair> pairs = geneNameValPairs(params);
		try {
			put.setEntity(new UrlEncodedFormEntity(pairs, charset));
			if (url.startsWith(HTTPS_PROTOCOL)) {
				initSSL(httpClient, getPort(url));
			}
			// 提交请求并以指定编码获取返回数据
			HttpResponse httpResponse = httpClient.execute(put);

			logger.info("请求地址:" + url + ";响应状态:" + httpResponse.getStatusLine());
			HttpEntity entity = httpResponse.getEntity();
			return EntityUtils.toString(entity, charset);
		} catch (ClientProtocolException e) {
			logger.error("协议异常,堆栈信息如下", e);
		} catch (IOException e) {
			logger.error("网络异常,堆栈信息如下", e);
		} finally {
			// 关闭连接,释放资源
			try {
				httpClient.close();
			} catch (Exception e) {
				e.printStackTrace();
				httpClient = null;
			}
		}
		return null;
	}

	/**
	 * 发送delete请求
	 * @param url		请求地址
	 * @param charset	返回数据编码
	 * @return
	 */
	public static String sendDelReq(String url, String charset) {
		if (null == charset)
			charset = DEFAULT_CHARSET;
		CloseableHttpClient httpClient = HttpClientBuilder.create().build();
		HttpDelete del = new HttpDelete(url);
		if (url.toLowerCase().startsWith(HTTPS_PROTOCOL)) {
			initSSL(httpClient, getPort(url));
		}
		try {
			// 提交请求并以指定编码获取返回数据
			HttpResponse httpResponse = httpClient.execute(del);
			logger.info("请求地址:" + url + ";响应状态:" + httpResponse.getStatusLine());
			HttpEntity entity = httpResponse.getEntity();
			return EntityUtils.toString(entity, charset);
		} catch (ClientProtocolException e) {
			logger.error("协议异常,堆栈信息如下", e);
		} catch (IOException e) {
			logger.error("网络异常,堆栈信息如下", e);
		} finally {
			// 关闭连接,释放资源
			try {
				httpClient.close();
			} catch (Exception e) {
				e.printStackTrace();
				httpClient = null;
			}
		}
		return null;
	}

	/**
	 * 发送POST请求,支持HTTP与HTTPS
	 * @param url 请求地址
	 * @param params 请求参数
	 * @param charset 返回数据编码
	 * @return 返回数据
	 */
	public static String sendPostReq(String url, Map<String, ?> params, String charset) {
		if (null == charset)
			charset = DEFAULT_CHARSET;
		CloseableHttpClient httpClient = HttpClientBuilder.create().build();
		RequestConfig reqConf = RequestConfig.DEFAULT;
		HttpPost httpPost = new HttpPost(url);
		// 封装请求参数
		List<NameValuePair> pairs = geneNameValPairs(params);
		try {
			httpPost.setEntity(new UrlEncodedFormEntity(pairs, charset));
			// 对HTTPS请求进行处理
			if (url.toLowerCase().startsWith(HTTPS_PROTOCOL)) {
				initSSL(httpClient, getPort(url));
			}
			// 提交请求并以指定编码获取返回数据
			httpPost.setConfig(reqConf);
			HttpResponse httpResponse = httpClient.execute(httpPost);
			int statuscode = httpResponse.getStatusLine().getStatusCode();
	        if ((statuscode == HttpStatus.SC_MOVED_TEMPORARILY) ||
	            (statuscode == HttpStatus.SC_MOVED_PERMANENTLY) ||
	            (statuscode == HttpStatus.SC_SEE_OTHER) ||
	            (statuscode == HttpStatus.SC_TEMPORARY_REDIRECT)) {
	        	Header header = httpResponse.getFirstHeader("location");
	            if (header != null) {
	                String newuri = header.getValue();
	                if ((newuri == null) || (newuri.equals("")))
	                    newuri = "/";
	                try {
	    				httpClient.close();
	    			} catch (Exception e) {
	    				e.printStackTrace();
	    				httpClient = null;
	    			}
	                return sendGetReq(newuri, null);
	            }
	        }

			logger.info("请求地址:" + url + ";响应状态:" + httpResponse.getStatusLine());
			HttpEntity entity = httpResponse.getEntity();
			return EntityUtils.toString(entity, charset);
		} catch (UnsupportedEncodingException e) {
			logger.error("不支持当前参数编码格式[" + charset + "],堆栈信息如下", e);
		} catch (ClientProtocolException e) {
			logger.error("协议异常,堆栈信息如下", e);
		} catch (IOException e) {
			logger.error("网络异常,堆栈信息如下", e);
		} finally {
			// 关闭连接,释放资源
			try {
				httpClient.close();
			} catch (Exception e) {
				e.printStackTrace();
				httpClient = null;
			}
		}
		return null;
	}

	/**
	 * 发送POST请求,支持HTTP与HTTPS, 参数放入请求体方式
	 * @param url 请求地址
	 * @param params 请求参数
	 * @param charset 返回数据编码
	 * @return 返回数据
	 */
	public static String sendPost(String url, String content, String charset) {
		if (null == charset)
			charset = DEFAULT_CHARSET;
		CloseableHttpClient httpClient = HttpClientBuilder.create().build();
		RequestConfig reqConf = RequestConfig.DEFAULT;
		HttpPost httpPost = new HttpPost(url);
		try {
			httpPost.setEntity(new StringEntity(content, charset));
			// 对HTTPS请求进行处理
			if (url.toLowerCase().startsWith(HTTPS_PROTOCOL)) {
				initSSL(httpClient, getPort(url));
			}
			// 提交请求并以指定编码获取返回数据
			httpPost.setConfig(reqConf);
			HttpResponse httpResponse = httpClient.execute(httpPost);
			int statuscode = httpResponse.getStatusLine().getStatusCode();
			if ((statuscode == HttpStatus.SC_MOVED_TEMPORARILY) ||
					(statuscode == HttpStatus.SC_MOVED_PERMANENTLY) ||
					(statuscode == HttpStatus.SC_SEE_OTHER) ||
					(statuscode == HttpStatus.SC_TEMPORARY_REDIRECT)) {
				Header header = httpResponse.getFirstHeader("location");
				if (header != null) {
					String newuri = header.getValue();
					if ((newuri == null) || (newuri.equals("")))
						newuri = "/";
					try {
						httpClient.close();
					} catch (Exception e) {
						e.printStackTrace();
						httpClient = null;
					}
					return sendGetReq(newuri, null);
				}
			}

			logger.info("请求地址:" + url + ";响应状态:" + httpResponse.getStatusLine());
			HttpEntity entity = httpResponse.getEntity();
			return EntityUtils.toString(entity, charset);
		} catch (UnsupportedEncodingException e) {
			logger.error("不支持当前参数编码格式[" + charset + "],堆栈信息如下", e);
		} catch (ClientProtocolException e) {
			logger.error("协议异常,堆栈信息如下", e);
		} catch (IOException e) {
			logger.error("网络异常,堆栈信息如下", e);
		} finally {
			// 关闭连接,释放资源
			try {
				httpClient.close();
			} catch (Exception e) {
				e.printStackTrace();
				httpClient = null;
			}
		}
		return null;
	}

	/**
	 * 初始化HTTPS请求服务
	 * @param httpClient HTTP客户端
	 */
	public static void initSSL(CloseableHttpClient httpClient, int port) {
		SSLContext sslContext = null;
		try {
			sslContext = SSLContext.getInstance("SSL");
			final X509TrustManager trustManager = new X509TrustManager() {
				public void checkClientTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {
				}

				public void checkServerTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {
				}

				public X509Certificate[] getAcceptedIssuers() {
					return null;
				}
			};
			// 使用TrustManager来初始化该上下文,TrustManager只是被SSL的Socket所使用
			sslContext.init(null, new TrustManager[] { trustManager }, null);
			ConnectionSocketFactory ssf = new SSLConnectionSocketFactory(sslContext);
			Registry<ConnectionSocketFactory> r = RegistryBuilder.<ConnectionSocketFactory>create().register("https", ssf).build();
			BasicHttpClientConnectionManager ccm = new BasicHttpClientConnectionManager(r);
			HttpClients.custom().setConnectionManager(ccm).build();
		} catch (KeyManagementException e) {
			e.printStackTrace();
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		}
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值