Commons HttpClient getResponse 需要很长的 200 秒

发布于 2024-11-27 13:22:11 字数 2185 浏览 1 评论 0原文

我没有改变这个方法,但突然需要很长时间。下面的代码示例会产生此错误。

编辑:我在单个java应用程序中提取了该方法并尝试加载该文件,并且我有相同的超时。第三或第四次,他经历这个循环 HttpMethodBase:691 他在我的本地电脑上停止了 500 秒,并且线程正在休眠。睡觉后,下一行是 outstream.close();

while ((len = instream.read(buffer)) > 0) {
   outstream.write(buffer, 0, len);
}

编辑: 如果您想在家尝试,这里是示例代码:) (httpClient 3.1)

import java.io.IOException;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.GetMethod;


public class TestLoadImage {

    /**
     * @param args
     */
    public static void main(String[] args) {
        byte[] image = loadPhotoFromUrl("https://fbcdn-sphotos-a.akamaihd.net/photos-ak-snc1/v2635/232/115/68310606562/n68310606562_2255479_948765.jpg");
        System.out.println(image.length);
    }

    private static class GetMethodIgnoringContentLength extends GetMethod {

        public GetMethodIgnoringContentLength(String uri) {
            super(uri);
        }

        @Override
        public long getResponseContentLength() {
            // ignores content-length header, fakes "not specified":
            return -1;
        }

    }
    public static byte[] loadPhotoFromUrl(String photoUrl) {
        HttpClient httpClient = new HttpClient();
        httpClient.getParams().setBooleanParameter("http.connection.stalecheck", true);
        GetMethod get = new GetMethodIgnoringContentLength(photoUrl);

        try {
            int httpStatus = httpClient.executeMethod(get);
            if (httpStatus == HttpStatus.SC_OK) {
                byte[] imageBytes = get.getResponseBody();
                if (imageBytes.length > 0) {
                    return imageBytes;
                } else {
                    System.out.println("Failed: empty response/zero data");
                }
            } else {
                System.out.println("Failed: "+ httpStatus);
            }
        } catch (IOException ioe) {
            System.out.println( ioe);
        } finally {
            get.releaseConnection();
        }

        return null;
    }
}

I changed nothing in this method but suddenly it takes very long. The code example below produces this error.

EDIT: i extracted the method in a single java application and tried to load the file and i have the same timeout. The 3 or 4. time he goes through this loop HttpMethodBase:691 he stops for 500 seconds at my local pc and the thread is sleeping. After sleeping the next line is outstream.close();

while ((len = instream.read(buffer)) > 0) {
   outstream.write(buffer, 0, len);
}

EDIT: Here is the example code if you wanna try it at home :) (httpClient 3.1)

import java.io.IOException;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.GetMethod;


public class TestLoadImage {

    /**
     * @param args
     */
    public static void main(String[] args) {
        byte[] image = loadPhotoFromUrl("https://fbcdn-sphotos-a.akamaihd.net/photos-ak-snc1/v2635/232/115/68310606562/n68310606562_2255479_948765.jpg");
        System.out.println(image.length);
    }

    private static class GetMethodIgnoringContentLength extends GetMethod {

        public GetMethodIgnoringContentLength(String uri) {
            super(uri);
        }

        @Override
        public long getResponseContentLength() {
            // ignores content-length header, fakes "not specified":
            return -1;
        }

    }
    public static byte[] loadPhotoFromUrl(String photoUrl) {
        HttpClient httpClient = new HttpClient();
        httpClient.getParams().setBooleanParameter("http.connection.stalecheck", true);
        GetMethod get = new GetMethodIgnoringContentLength(photoUrl);

        try {
            int httpStatus = httpClient.executeMethod(get);
            if (httpStatus == HttpStatus.SC_OK) {
                byte[] imageBytes = get.getResponseBody();
                if (imageBytes.length > 0) {
                    return imageBytes;
                } else {
                    System.out.println("Failed: empty response/zero data");
                }
            } else {
                System.out.println("Failed: "+ httpStatus);
            }
        } catch (IOException ioe) {
            System.out.println( ioe);
        } finally {
            get.releaseConnection();
        }

        return null;
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

当爱已成负担 2024-12-04 13:22:11

通过更新 HttpClient4.1 修复了这个问题(并且需要进行一些重构)。但如果有人知道上面例子中的原因,我就会改变正确的答案。

Fixed it with an update to HttpClient4.1 (and some refactoring required therefor). But if someone knows the reason in the example above i switch the correct answer.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文