HttpAndHttpsProxy接口访问工具类,支持代理

package com.unis.core;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.Proxy.Type;
import java.net.URL;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;

import org.apache.commons.codec.binary.Base64;
import org.json.JSONObject;

public class HttpAndHttpsProxy {


	/**
	 * HTTP请求
	 * @param url 请求的接口地址
	 * @param param 请求的接口参数
	 * @param isUseProxy 是否使用代理服务器
	 * @param proxy 代理服务器ip地址
	 * @param port 代理服务器端口号
	 * @return
	 */
	public static JSONObject HttpProxy(String url, String param, boolean isUseProxy, String proxy, int port) {
		HttpURLConnection httpConn = null;
		PrintWriter out = null;
		BufferedReader in = null;
		String result = "";
		BufferedReader reader = null;
		try {
			URL urlClient = new URL(url);
			if (isUseProxy) {
				httpConn = getHttpProxyConnection(urlClient, proxy, port);
			} else {
				httpConn = (HttpURLConnection) urlClient.openConnection();
			}
			// 设置通用的请求属性
			httpConn.setRequestProperty("accept", "*/*");
			httpConn.setRequestProperty("connection", "Keep-Alive");
			httpConn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
			// 发送POST请求必须设置如下两行
			httpConn.setDoOutput(true);
			httpConn.setDoInput(true);
			// 获取URLConnection对象对应的输出流
			out = new PrintWriter(httpConn.getOutputStream());
			// 发送请求参数
			out.print(param);
			// flush输出流的缓冲
			out.flush();
			// 定义BufferedReader输入流来读取URL的响应
			in = new BufferedReader(new InputStreamReader(httpConn.getInputStream()));
			String line;
			while ((line = in.readLine()) != null) {
				result += line;
			}
			// 断开连接
			httpConn.disconnect();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (reader != null) {
					reader.close();
				}
			} catch (IOException e) {
			}
			try {
				if (in != null) {
					in.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
			if (out != null) {
				out.close();
			}
		}

		return new JSONObject(result.toString());
	}

	
	
	/**
	 * 获取设置代理的HttpURLConnection
	 * @param url
	 * @return
	 */
	private static HttpURLConnection getHttpProxyConnection(URL url, String proxy, int port) {
		HttpURLConnection con = null;
		try {
			// 创建代理
			Proxy proxy1 = new Proxy(Type.HTTP, new InetSocketAddress(proxy, port));
			// 设置代理
			con = (HttpURLConnection) url.openConnection(proxy1);
			// 设置认证头部
			final String userName = "GW00165699";
			final String password = "123..com";
			String nameAndPass = userName + ":" + password;
			String encoding = new String(Base64.encodeBase64(nameAndPass.getBytes()));
			con.setRequestProperty("Proxy-Authorization", "Basic " + encoding);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return con;
	}

	
	/**
	 * HTTPS请求
	 * @param url 请求的接口地址
	 * @param param 请求的接口参数
	 * @param isUseProxy 是否使用代理服务器
	 * @param proxy 代理服务器ip地址
	 * @param port 代理服务器端口号
	 * @return
	 */
	
	public static JSONObject HttpsProxy(String url, String param, boolean isUseProxy, String proxy, int port) {
		HttpsURLConnection httpsConn = null;
		PrintWriter out = null;
		BufferedReader in = null;
		String result = "";
		BufferedReader reader = null;
		try {
			URL urlClient = new URL(url);
			if (isUseProxy) {
				httpsConn = getHttpsProxyConnection(urlClient, proxy, port);
			} else {
				httpsConn = (HttpsURLConnection) urlClient.getContent();
			}
			SSLContext sc = SSLContext.getInstance("SSL");
			// 指定信任https
			sc.init(null, new TrustManager[] { new TrustAnyTrustManager() }, new java.security.SecureRandom());
			httpsConn.setSSLSocketFactory(sc.getSocketFactory());
			httpsConn.setHostnameVerifier(new TrustAnyHostnameVerifier());
			// 设置通用的请求属性
			httpsConn.setRequestProperty("accept", "*/*");
			httpsConn.setRequestProperty("connection", "Keep-Alive");
			httpsConn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
			// 发送POST请求必须设置如下两行
			httpsConn.setDoOutput(true);
			httpsConn.setDoInput(true);
			// 获取URLConnection对象对应的输出流
			out = new PrintWriter(httpsConn.getOutputStream());
			// 发送请求参数
			out.print(param);
			// flush输出流的缓冲
			out.flush();
			// 定义BufferedReader输入流来读取URL的响应
			in = new BufferedReader(new InputStreamReader(httpsConn.getInputStream()));
			String line;
			while ((line = in.readLine()) != null) {
				result += line;
			}
			// 断开连接
			httpsConn.disconnect();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (reader != null) {
					reader.close();
				}
			} catch (IOException e) {
			}
			try {
				if (in != null) {
					in.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
			if (out != null) {
				out.close();
			}
		}

		return new JSONObject(result.toString());
	}

	
	
	/**
	 * 获取设置代理的HttpsURLConnection
	 * @param url
	 * @return
	 */
	private static HttpsURLConnection getHttpsProxyConnection(URL url, String proxy, int port) {
		HttpsURLConnection con = null;
		try {
			// 创建代理
			Proxy proxy1 = new Proxy(Type.HTTP, new InetSocketAddress(proxy, port));
			// 设置代理
			con = (HttpsURLConnection) url.openConnection(proxy1);
			// 设置认证头部
			final String userName = "GW00165699";
			final String password = "123..com";
			String nameAndPass = userName + ":" + password;
			String encoding = new String(Base64.encodeBase64(nameAndPass.getBytes()));
			con.setRequestProperty("Proxy-Authorization", "Basic " + encoding);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return con;
	}

	private static class TrustAnyTrustManager implements X509TrustManager {

		public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
		}

		public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
		}

		public X509Certificate[] getAcceptedIssuers() {
			return new X509Certificate[] {};
		}
	}

	private static class TrustAnyHostnameVerifier implements HostnameVerifier {
		public boolean verify(String hostname, SSLSession session) {
			return true;
		}
	}

}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

十五楼亮哥

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值