import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.Authenticator;
import java.net.HttpURLConnection;
import java.net.InetSocketAddress;
import java.net.PasswordAuthentication;
import java.net.Proxy;
import java.net.URL;
class ProxyAuthenticator extends Authenticator {
privateString user, password;
public ProxyAuthenticator(String user, String password) {
this.user = user;
this.password = password;
}
protected PasswordAuthentication getPasswordAuthentication() {
returnnew PasswordAuthentication(user, password.toCharArray());
}
}
/**
* 注意:下面代码仅仅实现HTTP请求链接,每一次请求都是无状态保留的,仅仅是这次请求是更换IP的,如果下次请求的IP地址会改变
* 如果是多线程访问的话,只要将下面的代码嵌入到你自己的业务逻辑里面,那么每次都会用新的IP进行访问,如果担心IP有重复,
* 自己可以维护IP的使用情况,并做校验。
*/public class ProxyDemo {
publicstaticvoid main(String args[]) throws Exception {
// 要访问的目标页面String targetUrl = "http://ip.hahado.cn/ip";
//String targetUrl = "http://ip.hahado.cn/switch-ip";//String targetUrl = "http://ip.hahado.cn/current-ip";// 代理服务器String proxyServer = "ip.hahado.cn";
int proxyPort = 39010;
// 代理隧道验证信息String proxyUser = "username";
String proxyPass = "password";
try {
URL url = new URL(targetUrl);
Authenticator.setDefault(new ProxyAuthenticator(proxyUser, proxyPass));
// 创建代理服务器地址对象
InetSocketAddress addr = new InetSocketAddress(proxyServer, proxyPort);
// 创建HTTP类型代理对象
Proxy proxy = new Proxy(Proxy.Type.HTTP, addr);
// 设置通过代理访问目标页面
HttpURLConnection connection = (HttpURLConnection) url.openConnection(proxy);
// 设置IP切换头
connection.setRequestProperty("Proxy-Switch-Ip","yes");
// 解析返回数据byte[] response = readStream(connection.getInputStream());
System.out.println(newString(response));
} catch (Exception e) {
System.out.println(e.getLocalizedMessage());
}
}
/**
* 将输入流转换成字符串
*
* @param inStream
* @return
* @throws Exception
*/publicstaticbyte[] readStream(InputStream inStream) throws Exception {
ByteArrayOutputStream outSteam = new ByteArrayOutputStream();
byte[] buffer = newbyte[1024];
int len = -1;
while ((len = inStream.read(buffer)) != -1) {
outSteam.write(buffer, 0, len);
}
outSteam.close();
inStream.close();
return outSteam.toByteArray();
}
}
JAVA HTTP代码示例-新手篇
最新推荐文章于 2024-02-01 09:33:26 发布