Java URL连接超时不工作
我正在使用UrlConnection访问本地Web服务器。 我测量每个请求所需的时间,有些请求最多需要200.000ms 即使我配置了超时,这怎么可能 我在Windows764位(Java764位)和Debian764位(Java764位)上进行了测试 是的,我在SO上阅读了相关问题/答案,但他们无法解决问题Java URL连接超时不工作,java,http,Java,Http,我正在使用UrlConnection访问本地Web服务器。 我测量每个请求所需的时间,有些请求最多需要200.000ms 即使我配置了超时,这怎么可能 我在Windows764位(Java764位)和Debian764位(Java764位)上进行了测试 是的,我在SO上阅读了相关问题/答案,但他们无法解决问题 public String download(String link) throws IOException { URL url = null;
public String download(String link) throws IOException {
URL url = null;
String html;
url = new URL(link);
resource = (HttpURLConnection) url.openConnection(Proxy.NO_PROXY);
resource.setConnectTimeout(2500);
resource.setReadTimeout(4500);
resource.addRequestProperty("Connection", "Keep-Alive");
resource.setRequestProperty("User-Agent","Analyzer");
resource.setRequestMethod("GET");
resource.connect();
if(resource.getResponseCode() == 200) {
is = resource.getInputStream();
String charset = resource.getContentEncoding();
if (charset == null)
charset = getCharset(resource.getContentType());
html = inputStreamToString(is, charset);
} else {
html = null;
}
//System.out.println(link+" => "+resource.getResponseCode());
resource.disconnect();
if(is != null) {
is.close();
}
return html;
}