URLConnection conn;
// 使用代理
InetSocketAddress addr = new InetSocketAddress("10.37.84.36", 8080);
Proxy proxy = new Proxy(Proxy.Type.HTTP, addr);
PrintWriter out = null;
String result = "";
BufferedReader in = null;
// 获取URLConnection对象对应的输出流
conn = realUrl.openConnection(proxy);
conn.setRequestProperty("Accept-Charset", "GBK");
conn.setRequestProperty("contentType", "GBK");
conn.setRequestProperty("accept", "*");
conn.setRequestProperty("connection", "Keep-Alive");
conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
// 发送POST请求必须设置如下两行
conn.setDoOutput(true);
conn.setDoInput(true);
// 获取URLConnection对象对应的输出流
out = new PrintWriter(conn.getOutputStream());
// 发送请求参数
out.print(makeXml());
// flush输出流的缓冲
out.flush();
// 定义BufferedReader输入流来读取URL的响应
in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "utf-8"));
String line;
System.out.println("======");
while ((line = in.readLine()) != null) {
result += new String(line);
System.out.println(result);
}
关于代理转发,代码如下具体还有待理解
猜你喜欢
转载自my.oschina.net/u/198077/blog/1787092
今日推荐
周排行