源码
import javaioBufferedInputStream;
import URL;
import URLConnection;
public class TestProxyIp {
public static void main(String args[]){
//SystemgetProperties()setProperty(proxySet true); //如果不设置只要代理IP和代理端口正确此项不设置也可以
SystemgetProperties()setProperty(httpproxyHost );
SystemgetProperties()setProperty(httpproxyPort );
Systemoutprintln(getHtml()); //判断代理是否设置成功
}
private static String getHtml(String address){
StringBuffer html = new StringBuffer();
String result = null;
try{
URL url = new URL(address);
URLConnection conn = urlopenConnection();
connsetRequestProperty(UserAgentMozilla/ (compatible; MSIE ; Windows NT ; GTB; NET CLR ; CIBA));
BufferedInputStream in = new BufferedInputStream(conngetInputStream());
try {
String inputLine;
byte[] buf = new byte[];
int bytesRead = ;
while (bytesRead >= ) {
inputLine = new String(buf bytesRead ISO);
htmlappend(inputLine);
bytesRead = inread(buf);
inputLine = null;
}
buf = null;
} finally {
inclose();
conn = null;
url = null;
}
result = new String(htmltoString()trim()getBytes(ISO) gb)toLowerCase();
}catch(Exception e){
eprintStackTrace();
return null;
}
html = null;
return result;
}
}
结果
说明:如果代理IP地址不正确将使用原有IP访问 如果代理端口不正确将无法访问