java使用代理髮送http請求

	//一種方法是設置全局的代理,在程序中如下設置:

	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();

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章