UrlConnection超时无效

时间:2013-06-30 14:37:49

标签: java http

我正在使用UrlConnection访问我的本地网络服务器。 我测量每个请求所需的时间,一些请求需要200.000ms。

即使我配置了超时,怎么会这样?

我测试了Windows 7 64位(Java 7 64位)& Debian 7 64位(Java 7 64位)

是的,我在SO上阅读了相关问题/答案,但他们无法解决问题。

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;
    }

0 个答案:

没有答案
相关问题