简体   繁体   中英

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. 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:

    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? 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

So the logs of the apache show me that the connection was successful and he indeed did get the request:

 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:

 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:

 [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? 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. You create a HTTP POST Request with the file as the request body.

The body of a filled in form POST HTTP Request looks like this:

user=Fritz&age=12

It looks sumilar like the URL params of a GET HTTP Request.

A filled in form POST with a file upload in it is a multipart HTTP Request with multiple bodies. One of it has the form fields the other the upload file content.

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

I cannot provide sample to set up your request, but this information may help you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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