i need send http post request mediafire rest api java web app running in google app engine, in order upload file.
please see upload function documentation here (few lines).
looking @ documentation , research, i've written following java code make correspondent request:
byte[] bytesdata = //byte array 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("content-type", "application/octet-stream"); conn.setrequestproperty("x-filename", filename); conn.setrequestproperty("x-filesize", filesize); conn.setrequestproperty("x-filehash", sha256); //write binary data system.out.println("\nwriting data..."); outputstream out = conn.getoutputstream(); out.write(bytesdata); out.flush(); //check connection //if (conn.getresponsecode() != httpurlconnection.http_created) { // throw new runtimeexception("failed!!! http error code: " + // conn.getresponsecode() + " --- " + // conn.getresponsemessage()); //} //get response system.out.println("\ngetting response..."); bufferedreader br = new bufferedreader(new inputstreamreader(conn.getinputstream())); //print response system.out.println("\noutput server .... \n"); string output; while ((output = br.readline()) != null) { system.out.println(output); }
but i'm not getting result, just:
writing data... getting response... output server:
if un-comment lines under //check connection
exception
:
java.lang.runtimeexception: failed!!! http error code: 503 --- ok
if change content-type
header multipart/form-data
different exception
:
java.lang.runtimeexception: failed!!! http error code: 400 --- ok
i don't know http connections , on , have no idea what's happening...
any ideas may going on?
note: i'm using other (get) methods of api same app , working perfectly.
http error code: 400 status code bad request, means server not except multipart/form-data, 503 code means server overloaded, service may hosted on different server/thread on server other methods, may receiving more traffic.
Comments
Post a Comment