java http 500错误怎么解决_java - HTTP POST请求中出现500内部错误

我正在使用以下代码通过HTTP POST上传文件,但是我从服务器收到500 Internal Server Error响应。

您能否看一下,让我知道哪个代码部分是罪魁祸首/缺失。 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");

}

谢谢

最佳答案

500 Internal Server Error是服务器错误,即。问题出在服务器端,而不是客户端。您需要检查服务器日志以查看问题所在。

header 很好。如果标题错误,则会收到400 Bad Request或其他4xx错误。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值