import java.net.*;
import java.io.*;
public class URLConnectionReader {
public static void main(String[] args) throws Exception {
URL yahoo = new URL("http://www.yahoo.com/");
URLConnection yc = yahoo.openConnection();
BufferedReader in = new BufferedReader(
new InputStreamReader(
yc.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null)
System.out.println(inputLine);
in.close();
}
}
堆栈跟踪与Connection timed out. Why?
相同我怀疑防火墙可能存在问题,但
如何更多地了解此问题? 我可以在运行代码时了解将使用哪些端口号吗?
由于
答案 0 :(得分:4)
找到贵公司使用的代理并在程序中进行设置。引用[1]
中的代码//Set the http proxy to webcache.mydomain.com:8080
System.setProperty("http.proxyHost", "webcache.mydomain.com");
System.setPropery("http.proxyPort", "8080");
// Next connection will be through proxy.
URL url = new URL("http://java.sun.com/");
InputStream in = url.openStream();
// Now, let's 'unset' the proxy.
System.setProperty("http.proxyHost", null);
// From now on http connections will be done directly.
[1] - http://docs.oracle.com/javase/6/docs/technotes/guides/net/proxies.html
答案 1 :(得分:0)
还有一个HttpURLConnection在某些情况下可能会更好。似乎没有以相同的方式断开连接。
URL url = new URL(address);
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setConnectTimeout(callTimeout);
以此类推。