【问题标题】:Jakarta commons HTTPClient can't send a post request on my apache server with phpJakarta commons HTTPClient 无法使用 php 在我的 apache 服务器上发送发布请求
【发布时间】:2013-08-08 14:16:13
【问题描述】:

我已经建立了一个 Apache 服务器,运行一个 php 脚本来处理各种 post 请求。脚本如下所示:

$uploaddir = realpath('mypath');
//realpath('./') . '/';
$uploadfile = $uploaddir . '/' . basename($_FILES['file_contents']['name']);
echo '<pre>';
if (move_uploaded_file($_FILES['file_contents']['tmp_name'], $uploadfile)) {
    echo "File is valid, and was successfully uploaded.\n";
} else {
    echo "Possible file upload attack!\n";
}
echo 'Here is some more debugging info:';
print_r($_FILES);
echo "\n<hr />\n";
print_r($_POST);
print "</pr" . "e>\n";

这很好用,它处理请求并将我的文件移动到我想要的位置。

但是当我用下面的 java 客户端尝试这个时,我什么也没得到:

    String URL = "http://localhost/accept.php";
    File file = new File("C:/Ozer/ANPRProject/MDT/Export/test.txt");

    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost(URL);

    try{
        System.out.println(""+file.exists());

        InputStreamEntity reqEntity = new InputStreamEntity(new FileInputStream(file),-1);

        reqEntity.setContentType("application/octet-stream");
        reqEntity.setChunked(true);

        httppost.setEntity(reqEntity);
        System.out.println("execute request " + httppost.getRequestLine());

        HttpResponse response = httpclient.execute(httppost);
        HttpEntity resEntity = response.getEntity();

        System.out.println("----------------------------------------");
        System.out.println(response.getStatusLine());

        if (resEntity != null) {
            System.out.println("Response content length: " + resEntity.getContentLength());
            System.out.println("Chunked: " + resEntity.isChunked());
        }
        EntityUtils.consume(resEntity);

    } catch(Exception e) {

    } finally {
        httpclient.getConnectionManager().shutdown();
    }
}

我得到的系统输出是:

 true
 execute request POST http://localhost/accept.php HTTP/1.1
 ----------------------------------------
 HTTP/1.1 200 OK
 Response content length: 330
 Chunked: false

我觉得很奇怪,因为 chunked 没有设置为 true?虽然我在我的代码中特别要求它。但无论如何,上传不起作用。我已将 contentType 设置为“application/octet-stream”,因为我用来请求帖子的 php 脚本给了我反馈:[type] => application/octet-stream

所以 apache 的日志显示连接成功,他确实收到了请求:

 127.0.0.1 - - [08/Aug/2013:16:10:04 +0200] "POST /accept.php HTTP/1.1" 200 330 "-" ""Apache-HttpClient/4.2.5 (java 1.5)"

这是我使用我的 PHP 脚本请求帖子时得到的结果:

 127.0.0.1 - - [08/Aug/2013:15:54:23 +0200] "POST /accept.php HTTP/1.1" 200 419 "-" "-" ::1 - - [08/Aug/2013:15:54:23 +0200] "GET /testSendDat.php HTTP/1.1" 200 3330 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.95 Safari/537.36"

来自 php 脚本的错误日志显示:

 [08-Aug-2013 16:10:04 Europe/Berlin] PHP Notice:  Undefined index: file_contents

所以我知道这意味着什么,我试图从中读取文件的数组是空的。但为什么?我没有任何线索。我猜我使用错误的方式来发布请求,但现在已经寻找了一段时间,并没有真正成功地找到问题所在。帮助将是非常有义务的。

哦,设置一个 apache tomcat 服务器并使用 httpservlets 会更容易吗?还是那不重要?

【问题讨论】:

  • 如果有人有兴趣,在这里发布后,我发现了一个类似的问题,这个人已经解决了他的问题。我知道我设置的帖子请求错误:stackoverflow.com/questions/1067655/…

标签: java php apache http


【解决方案1】:

您不会创建一个 HTTP 请求来模拟其中包含文件上传元素的填充表单。您创建一个 HTTP POST 请求,并将该文件作为请求正文。

已填写的 POST HTTP 请求的正文如下所示:

user=Fritz&age=12

它看起来很像 GET HTTP 请求的 URL 参数。

一个包含文件上传的表单 POST 是一个包含多个主体的多部分 HTTP 请求。其中一个具有表单字段,另一个具有上传文件内容。

见:how-does-http-file-upload-work

我无法提供样品来设置您的请求,但此信息可能会对您有所帮助。

【讨论】:

    猜你喜欢
    • 2022-11-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-01
    • 1970-01-01
    • 2019-08-10
    • 2023-03-04
    相关资源
    最近更新 更多