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;
}
}
}
HttpAndHttpsProxy接口访问工具类,支持代理
最新推荐文章于 2024-06-02 09:54:09 发布