Java:发送POST HTTP请求以将文件上载到MediaFire REST API
我需要从运行在Google app Engine中的Java web app向MediaFire REST API发送HTTP POST请求,以便上载文件 请参阅上传函数文档(几行) 通过查看文档和一些研究,我编写了以下Java代码来提出相应的请求:Java:发送POST HTTP请求以将文件上载到MediaFire REST API,java,http,rest,post,file-upload,Java,Http,Rest,Post,File Upload,我需要从运行在Google app Engine中的Java web app向MediaFire REST API发送HTTP POST请求,以便上载文件 请参阅上传函数文档(几行) 通过查看文档和一些研究,我编写了以下Java代码来提出相应的请求: byte[] bytesData = //byte array with file data URL url = new URL("http://www.mediafire.com/api/upload/upload.php?" +
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("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 from Server .... \n");
String output;
while ((output = br.readLine()) != null) {
System.out.println(output);
}
但我没有得到任何结果,只是:
Writing data...
Getting response...
Output from Server:
如果我取消注释//检查连接
下的行,我会得到一个异常
:
java.lang.RuntimeException: FAILED!!! HTTP error code: 503 --- OK
java.lang.RuntimeException: FAILED!!! HTTP error code: 400 --- OK
如果我更改多部分/表单数据的内容类型
标题
,我会得到不同的异常
:
java.lang.RuntimeException: FAILED!!! HTTP error code: 503 --- OK
java.lang.RuntimeException: FAILED!!! HTTP error code: 400 --- OK
我对HTTP连接等不太了解,也不知道发生了什么
对可能发生的事情有什么想法吗
注意:我正在使用来自同一个应用程序的API的其他(GET)方法,它们工作正常。HTTP错误代码:400是错误请求的状态代码,这可能意味着服务器除了多部分/表单数据之外没有其他功能,503代码意味着服务器过载,因此,该服务可能托管在服务器上与其他GET方法不同的服务器/线程上,而其他GET方法可能接收到更多流量