當前位置:才華齋>計算機>java語言>

java設定代理IP訪問網站的方法

java語言 閱讀(7.81K)

Java 技術具有卓越的通用性、高效性、平臺移植性和安全性,廣泛應用於PC、資料中心、遊戲控制檯、科學超級計算機、行動電話和網際網路,同時擁有全球最大的開發者專業社群。以下是小編為大家搜尋整理的設定代理IP訪問網站的方法,希望能給大家帶來幫助!更多精彩內容請及時關注我們應屆畢業生考試網!

java設定代理IP訪問網站的方法

1.原始碼

import eredInputStream;

import ;

import onnection;

public class TestProxyIp {

public static void main(String args[]){

//roperties()roperty("proxySet", "true"); //如果不設定,只要代理IP和代理埠正確,此項不設定也可以

roperties()roperty("yHost", "");

roperties()roperty("yPort", "8080");

tln(getHtml("")); //判斷代理是否設定成功

}

private static String getHtml(String address){

StringBuffer html = new StringBuffer();

String result = null;

try{

URL url = new URL(address);

URLConnection conn = Connection();

equestProperty("User-Agent","Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; GTB5; CLR 2.0.50727; CIBA)");

BufferedInputStream in = new BufferedInputStream(nputStream());

try {

String inputLine;

byte[] buf = new byte[4096];

int bytesRead = 0;

while (bytesRead >= 0) {

inputLine = new String(buf, 0, bytesRead, "ISO-8859-1");