I need to send a HTTP POST request to the MediaFire REST API from a Java web app running in Google App Engine in order to upload a file.
The documentation of the upload API says basically:
upload
URL: http://www.mediafire.com/api/upload/upload.php
Description : Upload a file through POST to the user's account. You can either use the session token to authenticate the user, or pass the dropbox key. This api returns the upload key when successful. You will have to pass this key to upload/poll_upload.php to get the Quickkey. Please refer to the documentation about the API upload/poll_upload for more details.
Required parameters
POST Data
- The file content (Binary data).
Optional Parameters
GET Data
- uploadkey : This is the folderkey of the target upload folder. If uploadkey is not passed, then the file will be uploaded to the root folder.
- session_token : A Session Access Token to authenticate the user's current session. Required if not uploading to a dropbox.
Header Data
- x-filename : File name you want to name the file on the system. If not passed, the original filename is used.
- x-filesize : File size of the file.
Looking at the documentation and some research, I've written the following Java code to make the correspondent request:
byte[] bytesData = //byte array with file data
URL url = new URL("http://www.mediafire.com/api/upload/upload.php?session_token=" + sessionToken);
//Configure connection
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.setRequestMethod("POST");
conn.setReadTimeout(60000);
//Set headers
conn.setRequestProperty("Accept", "application/json");
conn.setRequestProperty("Content-Type", "application/octet-stream");
conn.setRequestProperty("x-filename", fileName);
conn.setRequestProperty("x-filesize", fileSize);
//Write file
System.out.println("\nWriting data...");
OutputStream out = conn.getOutputStream();
out.write(bytesData);
out.flush();
//Get response
System.out.println("\nGetting response...");
BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
//Print results
System.out.println("\nOutput from server: \n");
String output;
while ((output = br.readLine()) != null) {
System.out.println(output);
}
But I'm not getting any result, just:
Writing data...
Getting response...
Output from Server:
I don't know too much about HTTP connections and so on and I have no idea about what's happening...
Furthermore, normally I use Chrome's developer tools to see the requests, but this call is made from a Java GAE application and I don't know how to monitorize the connection...
Any ideas about what may be going on?
Note: I'm using other (GET) methods of the API from the same app and they are working perfectly.