//一种方法是设置全局的代理,在程序中如下设置:
System.setProperty("proxySet", "true");
System.setProperty("http.proxyHost", "192.168.7.33");
System.setProperty("http.proxyPort", "8080");
//还有一种就是在发送每次请求的时候设置代理:
URL url = new URL("http://www.baidu.com");
Proxy proxy = new Proxy(Proxy.Type.DIRECT.HTTP, new InetSocketAddress("192.168.7.33", 8080));
HttpURLConnection conn = (HttpURLConnection) url.openConnection(proxy);
InputStream in = conn.getInputStream();
BufferedReader bin = new BufferedReader(new InputStreamReader(in));
String s = null;
while((s=bin.readLine())!=null){
System.out.println(s);
}
bin.close();
java使用代理发送http请求
最新推荐文章于 2024-10-18 18:10:28 发布