apache httpclient 4.0怪异的行为

uhry853o  于 2021-06-30  发布在  Java
关注(0)|答案(1)|浏览(351)

我正在使用apachehttpclient4.0作为我的网络爬虫。我发现奇怪的行为是:我试图通过httpget方法获取页面,并得到关于404http错误的响应。但如果我尝试使用浏览器获取该页面,它就会成功完成。
细节: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();
    }

加载成功结束。
我从web服务器得到一些结果:

HttpGet httpGet = new HttpGet("http://[host here]/res.php?key="+myKey+"&action=get&id="+id);

    HttpResponse response = httpClient.execute(httpGet);
    HttpEntity entity = response.getEntity();

我在执行方法运行时遇到clientprotocolexception。我在用log4j调试这个情况。服务器回答“404未找到”。但我的浏览器加载我的网页没有问题。
有人能帮我吗?
谢谢您。

gdx19jrr

gdx19jrr1#

我必须指出的是,这个问题与web服务器无关。如果我不将filebody添加到多部分表单数据中,则不会发生异常,没有http404,一切正常。

相关问题