上节,实现HTTP/HTTPS代理服务器,其中Http代理就是普通代理,https代理就是隧道代理,服务端已实现,本节的目标是,Java作为客户端是如何使用普通代理及隧道代理,案例如下。
场景,使用HTTP代理,发起http调用。
package com.what21.netty03.demo02;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.ProxySelector;
import java.net.URI;
import java.net.URL;
import java.util.List;
public class HttpProxyClientForHttp {
/**
* 通过系统属性设置代理
*
* @param proxyAddr
* @param proxyPort
*/
private static void setProxy(String proxyAddr, int proxyPort) {
System.setProperty("java.net.useSystemProxies", "true");
// HTTP代理
System.setProperty("http.proxyHost", proxyAddr);
System.setProperty("http.proxyPort", proxyPort + "");
// HTTPS代理
System.setProperty("https.proxyHost", proxyAddr);
System.setProperty("https.proxyPort", proxyPort + "");
}
/**
* @param uri
* @return
*/
private static Proxy getSystemProxy(String uri) {
Proxy proxy = null;
try {
ProxySelector ps = ProxySelector.getDefault();
List proxyList = ps.select(new URI(uri));
for (Proxy p : proxyList) {
// System.out.println("代理类型 : " + p.type());
InetSocketAddress addr = (InetSocketAddress) p.address();
if (addr == null) {
System.out.println("没有代理");
} else {
proxy = p;
System.out.println("代理主机 : " + addr.getHostName());
System.out.println("代理端口 : " + addr.getPort());
}
}
} catch (Exception e) {
e.printStackTrace();
}
return proxy;
}
/**
* @param use
*/
private static void invoke(boolean use) {
// 通过系统属性设置代理
setProxy("127.0.0.1",8888);
String urlStr = "http://www.baidu.com/";
// 获取代理
Proxy proxy = getSystemProxy(urlStr);
try {
URL url = new URL(urlStr);
HttpURLConnection conn = null;
if (use) {
// 使用代理代开资源
conn = (HttpURLConnection) url.openConnection(proxy);
System.out.println("使用了代理代开资源");
} else {
conn = (HttpURLConnection) url.openConnection();
}
conn.setRequestProperty("Content-Type", "text/xml; charset=UTF-8");
// 超时时间
conn.setConnectTimeout(10000);
InputStream is = conn.getInputStream();
BufferedReader in = new BufferedReader(new InputStreamReader(is));
StringBuffer text = new StringBuffer();
String line = null;
while ((line = in.readLine()) != null) {
text.append(line);
}
if (is != null) {
is.close();
}
if (conn != null) {
conn.disconnect();
}
// 打印内容
System.out.println(text.toString());
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* @param args
*/
public static void main(String[] args) {
invoke(false);
}
}
场景,使用HTTPS代理,发起https调用。
package com.what21.netty03.demo02;
import javax.net.ssl.HttpsURLConnection;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.Authenticator;
import java.net.InetSocketAddress;
import java.net.PasswordAuthentication;
import java.net.Proxy;
import java.net.URL;
public class HttpProxyClientForHttps {
/**
* 设置代理
*
* @param proxyAddr
* @param proxyPort
*/
private static Proxy createProxy(String proxyAddr, int proxyPort) {
// 设置认证
Authenticator.setDefault(new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("username", "password"
.toCharArray());
}
});
// 设置HTTP代理
Proxy proxy = new Proxy(Proxy.Type.HTTP,
new InetSocketAddress(proxyAddr, proxyPort));
return proxy;
}
/**
* @param use
*/
private static void invoke(boolean use) {
String urlStr = "https://www.baidu.com/";
// 创建代理对象
Proxy proxy = createProxy("127.0.0.1", 8888);
try {
URL url = new URL(urlStr);
HttpsURLConnection conn = null;
if (use) {
// 使用代理代开资源
conn = (HttpsURLConnection) url.openConnection();
System.out.println("使用了代理代开资源");
} else {
conn = (HttpsURLConnection) url.openConnection();
}
conn.setRequestProperty("Charset", "UTF-8");
conn.setRequestProperty("Content-Type", "text/xml; charset=UTF-8");
// 超时时间
conn.setConnectTimeout(10000);
InputStream is = conn.getInputStream();
BufferedReader in = new BufferedReader(new InputStreamReader(is));
StringBuffer text = new StringBuffer();
String line = null;
while ((line = in.readLine()) != null) {
text.append(line);
}
if (is != null) {
is.close();
}
if (conn != null) {
conn.disconnect();
}
// 打印内容
System.out.println(text.toString());
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* @param args
*/
public static void main(String[] args) {
invoke(true);
}
}
版权声明:我们致力于保护作者版权,注重分享,被刊用文章【http 在线代理(Java)】因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理!;
工作时间:8:00-18:00
客服电话
电子邮件
beimuxi@protonmail.com
扫码二维码
获取最新动态
