我使用下面的代码使用HTTP POST上传文件,但我从服务器获得500内部服务器错误响应.
你能看看,让我知道哪个代码部分是罪魁祸首/丢失.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); …Run Code Online (Sandbox Code Playgroud)