简体   繁体   English

Jakarta Commons HTTPClient无法使用php在我的Apache服务器上发送发布请求

[英]Jakarta commons HTTPClient can't send a post request on my apache server with php

I have set up a Apache server, with a php script running to handle various post requests. 我已经建立了一个Apache服务器,并运行一个php脚本来处理各种发布请求。 the script looks as followed: 该脚本如下所示:

$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";

and this works great, it handles the request and moves my file to where i want it. 而且效果很好,它可以处理请求并将文件移到我想要的位置。

But when i try this with following java client, i dont get anything: 但是当我尝试使用以下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();
    }
}

What I get as system out put is: 我得到的系统输出是:

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

Which I find weird, because the chunked has not been set to true? 我觉得很奇怪,因为未将分块设置为true? While i specifically asked for it in my code. 虽然我在代码中特别要求它。 But anyhow, the uploading does not work. 但是无论如何,上传是行不通的。 I Have set the contentType to "application/octet-stream" because the php script I used to request a post gave me as feedback: [type] => application/octet-stream 我已将contentType设置为“ application / octet-stream”,因为我曾经请求发布的php脚本给了我反馈:[type] => application / octet-stream

So the logs of the apache show me that the connection was successful and he indeed did get the request: 因此,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)"

this is what i get when i use my Php script to request a post: 这是我使用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"

and the error log from the php script shows me: 和来自PHP脚本的错误日志显示给我:

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

So I know what that means, the array where i'm trying to read my files from are empty. 所以我知道这意味着什么,我要从中读取文件的数组为空。 But why? 但为什么? I have no clue. 我没有任何线索。 I'm guessing I'm using the wrong way to post a request but have been looking for a while now and haven't really been successful in finding what the problem could be. 我猜我使用的是错误的方式来发布请求,但是现在已经寻找了一段时间,而实际上并没有成功地找到问题所在。 Help would be much obliged. 帮助将非常有必要。

Oh and would it perhaps be easier to set up a apache tomcat server and work with httpservlets? 哦,设置apache tomcat服务器并使用httpservlets可能会更容易些吗? Or would'nt that matter? 还是那不重要吗?

You do not create a HTTP-Request that simulate a filled in form with a file upload element in it. 您不会创建一个HTTP-Request来模拟其中带有文件上载元素的已填写表单。 You create a HTTP POST Request with the file as the request body. 您使用文件作为请求正文创建HTTP POST请求。

The body of a filled in form POST HTTP Request looks like this: 填写的表单POST HTTP Request的正文如下所示:

user=Fritz&age=12

It looks sumilar like the URL params of a GET HTTP Request. 它看起来像GET HTTP请求的URL参数。

A filled in form POST with a file upload in it is a multipart HTTP Request with multiple bodies. 带有文件上传的已填写表单POST是包含多个正文的多部分HTTP请求。 One of it has the form fields the other the upload file content. 其中一个具有表单字段,另一个具有上载文件内容。

see: how-does-http-file-upload-work 请参阅: http文件上传工作方式

I cannot provide sample to set up your request, but this information may help you. 我无法提供样本来设置您的请求,但是此信息可能会对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM