import java.io.IOException;
import java.io.InputStream;
import java.net.Authenticator;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.PasswordAuthentication;
import java.net.URL;
import java.util.Properties;
public class HttpProxy {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Properties prop = System.getProperties();
prop.put("proxySet","true");
// 设置http访问要使用的代理服务器的地址
prop.setProperty("http.proxyHost", "130.194.11.121");
// 设置http访问要使用的代理服务器的端口
prop.setProperty("http.proxyPort", "1080");
prop.list(System.out);
// 设置不需要通过代理服务器访问的主机,可以使用*通配符,多个地址用|分隔
//prop.setProperty("http.nonProxyHosts", "localhost|192.168.0.*");
// 设置安全访问使用的代理服务器地址与端口
// 它没有https.nonProxyHosts属性,它按照http.nonProxyHosts 中设置的规则访问
/*
prop.setProperty("https.proxyHost", "192.168.0.254");
prop.setProperty("https.proxyPort", "443");
*/
// 使用ftp代理服务器的主机、端口以及不需要使用ftp代理服务器的主机
/*
prop.setProperty("ftp.proxyHost", "192.168.0.254");
prop.setProperty("ftp.proxyPort", "2121");
prop.setProperty("ftp.nonProxyHosts", "localhost|192.168.0.*");
*/
// socks代理服务器的地址与端口
/*
prop.setProperty("socksProxyHost", "192.168.0.254");
prop.setProperty("socksProxyPort", "8000");
*/
// 设置登陆到代理服务器的用户名和密码
Authenticator.setDefault(new MyAuthenticator("", ""));
URL url;
HttpURLConnection http;
InputStream urlstream;
try {
url = new URL("http://www.blogger.com/");
http = (HttpURLConnection)url.openConnection();
http.connect();
urlstream = http.getInputStream();
byte buffer[] = new byte[1024];
int i;
while ((i =urlstream.read(buffer)) != -1) {
System.out.write(buffer, 0, i);
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
通过代理访问一些网址
最新推荐文章于 2024-05-09 16:19:10 发布