Tôi đang sử dụng mã bên dưới để tải lên tệp bằng cách sử dụng HTTP POST, nhưng tôi nhận được 500 phản hồi Lỗi Máy chủ Nội bộ từ máy chủ.500 Lỗi nội bộ từ yêu cầu HTTP POST
Bạn có thể vui lòng xem và cho tôi biết phần mã nào là thủ phạm/thiếu. Không có lỗi trong kết nối HTTPS, tôi nghĩ rằng một số vấn đề trong Tiêu đề để máy chủ không chấp nhận yêu cầu này.
// Check server address
url = new URL("https://example.com");
String protocol = url.getProtocol();
String host = url.getHost();
String serviceRoot = url.getPath();
// Build POST request
HttpPost post = new HttpPost(new URI(protocol + "://" + host
+ serviceRoot));
post.addHeader("User-Agent", "Test");
post.addHeader("Content-type", "multipart/form-data");
post.addHeader("Accept", "image/jpg");
String authValue = "Basic "
+ Base64
.encodeBase64ToString(("username" + ":"
+ "password").getBytes()) + " " + "realm=\"example.com\"";
if (authValue != null) {
post.addHeader("Authorization", authValue);
}
File file = new File("/sdcard/Download/IMAG0306.jpg");
FileBody data = new FileBody(file);
String file_type = "jpg" ;
String description = "Test";
MultipartEntity reqEntity = new MultipartEntity();
reqEntity.addPart("file_name", new StringBody(file.getName()));
reqEntity.addPart("description", new StringBody(description));
reqEntity.addPart("file_type", new StringBody(file_type));
reqEntity.addPart("data", data);
post.setEntity(reqEntity);
if (true) {
String trace = ">>> Send HTTP request:";
trace += "\n " + post.getMethod() + " "
+ post.getRequestLine().getUri();
System.out.println(trace);
}
if (true) {
String trace = "<<< Send HTTP request-->:";
trace += "\n" + post.toString();
Header[] headers = post.getAllHeaders();
for (Header header : headers) {
trace += "\n" + header.getName() + " " + header.getValue();
}
System.out.println(trace);
}
HttpClient httpClient = createHttpClient();
// replace with your url
// “Authorization”, “Basic ” + encodedUsernamePassword);
if (httpClient != null) {
response = httpClient.execute(post);
if (true) {
String trace = "<<< Receive HTTP response:";
trace += "\n" + response.getStatusLine().toString();
Header[] headers = response.getAllHeaders();
for (Header header : headers) {
trace += "\n" + header.getName() + " " + header.getValue();
}
System.out.println(trace);
}
} else {
throw new IOException("HTTP client not found");
}
Cảm ơn
Hãy xem nhật ký máy chủ của bạn. 500 là lỗi máy chủ nội bộ. Vì vậy, bạn nên tìm giải pháp ở đó. – Jens
500 là lỗi phía máy chủ, bạn có thể kiểm tra yêu cầu của mình trong ứng dụng khách http còn lại (ví dụ: tiện ích bổ sung cho trình duyệt), với cùng các thông số chính xác, nó sẽ kết thúc với cùng một kết quả. – Mostrapotski
Săn tìm của tôi là yêu cầu của bạn vượt quá MaxRequestLen, thường là 131072 byte, bạn có thể thử với một tệp nhỏ để xem nó có hoạt động hay không, nhưng để hoàn toàn chắc chắn là các bản ghi lỗi máy chủ của bạn trong khi đưa ra yêu cầu. tail -f /var/log/apache2/error.log –