简体   繁体   中英

Sending HTTP POST multipart/form-data from Android client to HTTP server

I am writing an app that will allow the user to take a picture, which is automatically sent to an HTTP server to be classified via HTTP POST multipart/form-data. To build and send these requests, I am using Apache's HTTPClient library. I am somewhat confident that my Java clientside code works, as I have tested with one of the Imgur API endpoints and the images uploaded correctly; therefore, I think it is most likely that there is an error in the endpoint on my server. I am sending the request to server to be received by a PHP script that I wrote to receive the file, save it to directory on the server, and send a response back to the client appropriately. When I test this, the server always sends back a response with status code 200, even though the script obviously is not working. I have tried manually setting the response status code to 500 to force an error code, and even that doesn't work, which makes me think that the entire script does not even run when the request hits the endpoint (if it even hits the endpoint). I expect this to be either an error in server configuration, or an error in the script itself. However, I have pasted the Java code just in case. You may find some errors there too. I am not great with networking or with PHP, so I could definitely use some more experienced eyes.

  • Note: I know my PHP code looks awful. You will probably find a lot of bad practices; sorry.

My server code:

<?php

$path = '/home/user01/Documents/uploaded_images/';
$ext = '.jpg';

function getFileName() {
  return 'image_'.date('Y-m-d_His');
}

//Receive the data from android client

$file = $_FILES['uploadedFile'];


//process the data

$filename = $path . getFileName() . '_' . $_SERVER['REMOTE_ADDR'] . $ext;

if (!is_uploaded_file($file['tmp_name']) ||
    !copy($file['tmp_name'], $filename))
{
  $message = 'Could not save file as' . $filename;
  $error = True;
}
else {
  $message = "Image uploaded successfully!";
  $error = False;
}

//return response to the server

if ($error) {
  http_response_code(500);
}

echo json_encode(
  array(
    'status' => 'Repsonse Code :' . http_response_code(),
    'message' => $message
  ), JSON_FORCE_OBJECT);


?>

Update and Resolution:

After some troubleshooting with the help of a few of the post commenters, I first tried writing a simple HTML form to submit POST requests locally to the PHP script, and after those requests failed, I then added the die() function at the beginning of the code, and still, nothing happened. This allowed me to figure out that the error.

The problems that I was having were caused by a bad server configuration. It was causing PHP to not be recognized as an installed language; therefore, the script was not being executed at all. Once the configuration was fixed, the script worked exactly as expected.

Much kudos to the commenters for their insight.

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