public static void main(String[] args){
//用于创建一个代理的链接
SocketAddress sa = new InetSocketAddress("IP",端口号);
//生成一个代理对象... 代理模式为Proxy.Type.HTTP
Proxy proxy = new Proxy(Proxy.Type.HTTP, sa);
try {
//创建一个URL对象
URL url = new URL("www.baidu.com");
//通过创建的代理用于打开指定的网址
HttpURLConnection con = (HttpURLConnection) url.openConnection(proxy);
//设置编码信息获取到的信息用utf-8编码
con.setRequestProperty("Charsert", "UTF-8");
BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream(), "UTF-8"));
String str = br.readLine();
System.out.println(str);
} catch (UnsupportedEncodingException e) {
System.out.println("编码格式异常");
} catch (IOException e) {
System.out.println("超时");
}
}
Last modification:June 25th, 2018 at 05:38 pm
© The copyright belongs to the author
Comment here is closed