【发布时间】:2019-06-25 21:13:45
【问题描述】:
我正在尝试使用以下代码连接 Google.com,但得到了
java.net.ConnectException:连接超时。 请帮忙!
private static String getUrlContents(String theUrl) {
StringBuilder content = new StringBuilder();
// many of these calls can throw exceptions, so i've just
// wrapped them all in one try/catch statement.
try {
// create a url object
URL url = new URL(theUrl);
// create a urlconnection object
URLConnection urlConnection = url.openConnection();
// wrap the urlconnection in a bufferedreader
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
String line;
// read from the urlconnection via the bufferedreader
while ((line = bufferedReader.readLine()) != null) {
content.append(line + "\n");
}
bufferedReader.close();
} catch (Exception e) {
e.printStackTrace();
}
return content.toString();
}
【问题讨论】:
-
网址是
http://www.google.com? -
你的电脑和互联网之间有防火墙吗?
标签: java exception httpurlconnection