Apache HttpClient 4.0。奇怪的行为

发布于 2024-08-10 05:30:59 字数 1420 浏览 1 评论 0原文

我正在使用 Apache HttpClient 4.0 作为我的网络爬虫。我发现奇怪的行为是:我试图通过 HTTP GET 方法获取页面并获取有关 404 HTTP 错误的响应。但如果我尝试使用浏览器获取该页面,它就成功完成了。

细节: 1.我以这种方式将多部分表单上传到服务器:

    HttpPost httpPost = new HttpPost("http://[host here]/in.php");

    MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
    entity.addPart("method", new StringBody("post"));
    entity.addPart("key", new StringBody("223fwe0923fjf23"));
    FileBody fileBody = new FileBody(new File("photo.jpg"), "image/jpeg");
    entity.addPart("file", fileBody);
    httpPost.setEntity(entity);

    HttpResponse response = httpClient.execute(httpPost);       
    HttpEntity result = response.getEntity();

    String responseString = "";
    if (result != null) {
        InputStream inputStream = result.getContent();

        byte[] buffer = new byte[1024];
        while(inputStream.read(buffer) > 0)
            responseString += new String(buffer);

        result.consumeContent();
    }

上传成功结束。

  1. 我从网络服务器获得一些结果:

     HttpGet httpGet = new HttpGet("http://[此处的主机]/res.php?key="+myKey+"&action=get&id="+id);
    
        HttpResponse 响应 = httpClient.execute(httpGet);
        HttpEntity实体=response.getEntity();
    

我在执行方法运行时收到 ClientProtocolException。我正在使用 log4j 调试这种情况。服务器回答“404 Not Found”。但我的浏览器加载该页面没有任何问题。

有人可以帮助我吗?

谢谢。

I'm using Apache HttpClient 4.0 for my web crawler. The behavior i found strange is: i'm trying to get page via HTTP GET method and getting response about 404 HTTP error. But if i try to get that page using browser it's done successfully.

Details:
1. I upload multipart form to server this way:

    HttpPost httpPost = new HttpPost("http://[host here]/in.php");

    MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
    entity.addPart("method", new StringBody("post"));
    entity.addPart("key", new StringBody("223fwe0923fjf23"));
    FileBody fileBody = new FileBody(new File("photo.jpg"), "image/jpeg");
    entity.addPart("file", fileBody);
    httpPost.setEntity(entity);

    HttpResponse response = httpClient.execute(httpPost);       
    HttpEntity result = response.getEntity();

    String responseString = "";
    if (result != null) {
        InputStream inputStream = result.getContent();

        byte[] buffer = new byte[1024];
        while(inputStream.read(buffer) > 0)
            responseString += new String(buffer);

        result.consumeContent();
    }

Uppload succefully ends.

  1. I'm getting some results from web server:

        HttpGet httpGet = new HttpGet("http://[host here]/res.php?key="+myKey+"&action=get&id="+id);
    
        HttpResponse response = httpClient.execute(httpGet);
        HttpEntity entity = response.getEntity();
    

I'm getting ClientProtocolException while execute method run. I was debugging this situation with log4j. Server answers "404 Not Found". But my browser loads me that page with no problem.

Can anybody help me?

Thank you.

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

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

发布评论

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

评论(1

我偏爱纯白色 2024-08-17 05:30:59

我必须注意这个问题与网络服务器无关。如果我不将 FileBody 添加到多部分表单数据中,则不会发生异常,一切正常,没有 HTTP 404。

I have to note the problem isn't concerned with web server. If i don't add FileBody to multipart form data, exception doesn't occure, all goes fine with no HTTP 404.

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