public void hasProxy(){
try{
System.setProperty("proxySet", "true");
System.setProperty("http.proxyHost", "11.0.1.210");
System.setProperty("http.proxyPort", "8080");
URL u = new URL("http://www.baidu.com");
HttpURLConnection conn = (HttpURLConnection)u.openConnection(); String authentication = "caixian2:96191264";
String encodedLogin = new BASE64Encoder().encodeBuffer(authentication.getBytes());
encodedLogin = encodedLogin.replaceAll("\n", "");
conn.setRequestProperty("Proxy-Authorization", "Basic " + encodedLogin);
conn.connect();
int length = conn.getContentLength(); // <- 程序执行到这里的时候就死掉了,但是如果我访问我自己电脑上tomcat建立的服务的话就可以,这是为什么呢?
System.out.println(length);
InputStream is = conn.getInputStream();
byte[] b = new byte[length];
is.read(b);
for(int i = 0; i < b.length; i++){
System.out.print((char)b[i]);
}
}catch(IOException e){
e.printStackTrace();
}
}
上面是程序的执行代码,我只是把它从main里面摘出来了。
程序执行到了 代码注释部分的时候就卡死了,什么结果也没有,高人们给个指点哈。
非常感谢!
try{
System.setProperty("proxySet", "true");
System.setProperty("http.proxyHost", "11.0.1.210");
System.setProperty("http.proxyPort", "8080");
URL u = new URL("http://www.baidu.com");
HttpURLConnection conn = (HttpURLConnection)u.openConnection(); String authentication = "caixian2:96191264";
String encodedLogin = new BASE64Encoder().encodeBuffer(authentication.getBytes());
encodedLogin = encodedLogin.replaceAll("\n", "");
conn.setRequestProperty("Proxy-Authorization", "Basic " + encodedLogin);
conn.connect();
int length = conn.getContentLength(); // <- 程序执行到这里的时候就死掉了,但是如果我访问我自己电脑上tomcat建立的服务的话就可以,这是为什么呢?
System.out.println(length);
InputStream is = conn.getInputStream();
byte[] b = new byte[length];
is.read(b);
for(int i = 0; i < b.length; i++){
System.out.print((char)b[i]);
}
}catch(IOException e){
e.printStackTrace();
}
}
上面是程序的执行代码,我只是把它从main里面摘出来了。
程序执行到了 代码注释部分的时候就卡死了,什么结果也没有,高人们给个指点哈。
非常感谢!
中间延迟一下,看看会不会有效果
int length = conn.getContentLength();
conn.connect();
wait(5000);
int length = conn.getContentLength();
但是和之前运行的结果一样。
抓包~
把往proxy去的和回来的都抓回来,再分析.
干等着也不是办法呀~
SocketAddress addr = new InetSocketAddress(host, port);
Proxy typeProxy = new Proxy(Proxy.Type.HTTP, addr);
URL url = new URL("http://www.163.com");
URLConnection conn = url.openConnection(typeProxy);
if (conn == null)
return;
conn.setConnectTimeout(1500); // 设置连接超时时间
System.out.println(System.currentTimeMillis()+"------------"+count);
conn.setReadTimeout(8000);
InputStream in = conn.getInputStream();
BufferedReader br = new BufferedReader(new java.io.InputStreamReader(in));给你点参考,我以前写的一个代理
我用的是1.4.2JDK,这个里面的conn并没有你说的 conn.setConnectTimeout(1500); 和 conn.setReadTimeout(8000); 两个方法,而且根本没有和时间有关的方法。对于这位兄弟,真是不好意思,这个。如何抓包呢,我是第一次用URL通过代理来方位广域网,而且对这类方法都不是很熟悉,你的意思是不是要我写socket啊?其实最主要的是我现在不明白为什么会出险这种现象,我debug了一下,发现在 conn.connect() 的时候会等待2-3秒左右,不过还是可以向下继续进行的,可是到了int length = conn.getContentLength(); 或者 InputStream is = conn.getInputStream(); 的时候,——也就是我要取conn里面值的时候——,程序就卡住了,还不是死掉了,就像进入死循环或者被无限的sleep一样!郁闷啊!!!
我觉得你可以首先看能否ping 通11.0.1.210 的8080 端口,
然后看能否用IE代理上网
然后再用程序测试,