Java 来自 HTTP POST 请求的 500 内部错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30258554/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
500 Internal error from HTTP POST request
提问by Arpit
I am using below code to upload file using HTTP POST, but I am getting 500 Internal Server Error response from server.
我正在使用以下代码使用 HTTP POST 上传文件,但我从服务器收到 500 内部服务器错误响应。
Can you please have a look and let me know which code part is culprit/missing. There is no error in HTTPS connection, I think some problem in Header so server is not accepting this request.
你能看看,让我知道哪个代码部分是罪魁祸首/丢失。HTTPS 连接没有错误,我认为 Header 中存在一些问题,因此服务器不接受此请求。
// 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");
}
Thanks
谢谢
采纳答案by StenSoft
500 Internal Server Error is a server error, ie. the problem is on the server side, not client side. You need to check the server logs to see what the problem is.
500 Internal Server Error 是服务器错误,即。问题出在服务器端,而不是客户端。您需要检查服务器日志以查看问题所在。
The headers are fine. If the headers were wrong, you would get 400 Bad Request or some other 4xx error instead.
标题很好。如果标头错误,您将收到 400 Bad Request 或其他一些 4xx 错误。