我正在使用以下代码通过HTTP POST上传文件,但是我从服务器收到500 Internal Server Error响应。
您能否看一下,让我知道哪个代码部分是罪魁祸首/缺失。 HTTPS连接中没有错误,我认为 header 中存在一些问题,因此服务器不接受此请求。// Check server address
url = new URL("");
String protocol = ();
String host = ();
String serviceRoot = ();
// Build POST request
HttpPost post = new HttpPost(new URI(protocol + "://" + host
+ serviceRoot));
("User-Agent", "Test");
("Content-type", "multipart/form-data");
("Accept", "image/jpg");
String authValue = "Basic "
+ Base64
.encodeBase64ToString(("username" + ":"
+ "password").getBytes()) + " " + "realm=\"\"";
if (authValue != null) {
("Authorization", authValue);
}
File file = new File("/sdcard/Download/");
FileBody data = new FileBody(file);
String file_type = "jpg" ;
String description = "Test";
MultipartEntity reqEntity = new MultipartEntity();
("file_name", new StringBody( () ) );
("description", new StringBody(description));
("file_type", new StringBody(file_type));
("data", data);
(reqEntity);
if (true) {
String trace = ">>> Send HTTP request:";
trace += "\n " + () + " "
+ ().getUri();
(trace);
}
if (true) {
String trace = "<<< Send HTTP request-->:";
trace += "\n" + ();
Header[] headers = ();
for (Header header : headers) {
trace += "\n" + () + " " + ();
}
(trace);
}
HttpClient httpClient = createHttpClient();
// replace with your url
// “Authorization”, “Basic ” + encodedUsernamePassword);
if (httpClient != null) {
response = (post);
if (true) {
String trace = "<<< Receive HTTP response:";
trace += "\n" + ().toString();
Header[] headers = ();
for (Header header : headers) {
trace += "\n" + () + " " + ();
}
(trace);
}
} else {
throw new IOException("HTTP client not found");
}
谢谢
最佳答案
500 Internal Server Error是服务器错误,即。问题出在服务器端,而不是客户端。您需要检查服务器日志以查看问题所在。
header 很好。如果标题错误,则会收到400 Bad Request或其他4xx错误。