JAVA HTTP代码示例-新手篇
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JAVA HTTP代码示例-新手篇相关的知识,希望对你有一定的参考价值。
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
private String user, password;
public ProxyAuthenticator(String user, String password)
this.user = user;
this.password = password;
protected PasswordAuthentication getPasswordAuthentication()
return new PasswordAuthentication(user, password.toCharArray());
/**
* 注意:下面代码仅仅实现HTTP请求链接,每一次请求都是无状态保留的,仅仅是这次请求是更换IP的,如果下次请求的IP地址会改变
* 如果是多线程访问的话,只要将下面的代码嵌入到你自己的业务逻辑里面,那么每次都会用新的IP进行访问,如果担心IP有重复,
* 自己可以维护IP的使用情况,并做校验。
*/
public class ProxyDemo
public static void 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(new String(response));
catch (Exception e)
System.out.println(e.getLocalizedMessage());
/**
* 将输入流转换成字符串
*
* @param inStream
* @return
* @throws Exception
*/
public static byte[] readStream(InputStream inStream) throws Exception
ByteArrayOutputStream outSteam = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = -1;
while ((len = inStream.read(buffer)) != -1)
outSteam.write(buffer, 0, len);
outSteam.close();
inStream.close();
return outSteam.toByteArray();
以上是关于JAVA HTTP代码示例-新手篇的主要内容,如果未能解决你的问题,请参考以下文章
Java 四种线程池newCachedThreadPool,newFixedThreadPool,newScheduledThreadPool,newSingleThreadExecutor(示例代码