我正在运行一个简单的java程序来连接到硬编码的URL "http://www.google.co.in/?gws_rd=ssl“,在尝试获取响应代码时:它抛出了连接超时异常。
代码片段如下:
url = new URL("http://www.google.co.in/?gws_rd=ssl");
urlConn = (HttpURLConnection) url.openConnection();
**int responseCode = urlConn.getResponseCode(); ---> exception line**如果谁有解决方案,请帮忙?
发布于 2015-10-26 13:37:28
你和urlConn = (HttpsURLConnection) url.openConnection();确认过了吗。
发布于 2015-10-26 13:54:38
试试这个,
public static String callURL(String myURL) {
System.out.println("Requeted URL:" + myURL);
StringBuilder sb = new StringBuilder();
URLConnection urlConn = null;
InputStreamReader in = null;
try {
URL url = new URL(myURL);
urlConn = url.openConnection();
if (urlConn != null)
urlConn.setReadTimeout(60 * 1000);
if (urlConn != null && urlConn.getInputStream() != null) {
in = new InputStreamReader(urlConn.getInputStream(),
Charset.defaultCharset());
BufferedReader bufferedReader = new BufferedReader(in);
if (bufferedReader != null) {
int cp;
while ((cp = bufferedReader.read()) != -1) {
sb.append((char) cp);
}
bufferedReader.close();
}
}
in.close();
} catch (Exception e) {
throw new RuntimeException("Exception while calling URL:"+ myURL, e);
}
return sb.toString();
}发布于 2015-10-26 15:05:11
你在代理背后的可证明的原因。确认您能够使用浏览器连接到该URL。设置JVM的代理设置。这是设置代理设置的link。
https://stackoverflow.com/questions/33338990
复制相似问题