500 Internal HTTP POST request error

I use the code below to upload a file using HTTP POST, but I get 500 Internal Server Error from the server.

Could you take a look and tell me which part of the code is guilty / missing. There is no error in the HTTPS connection, I think that some problem is in the header, so the server does not accept this request.

// 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

+7
java android
source share
1 answer

500 Internal server error is a server error, i.e. the problem is on the server side, not on the client side. You need to check the server logs to find out what the problem is.

The headings are fine. If the headers are incorrect, you will receive 400 Bad Request or another 4xx error.

+9
source

All Articles