Apache Httpclient 4.5.6 中的多部分 Post 请求不起作用
Multipart Post request in Apache Httpclient 4.5.6 not working
我正在尝试将文件上传到服务器。使用 Postman 工具时请求工作正常。
在 Postman 中我有:
POST :https://transport-stg.transperfect.com/api/files/upload?filename=test.png&targetFolderId=2ff5ea6a-8187-4622-ab58-c7511c445ae7
在 Header 我有 :
Content-Type = multipart/form-data
Authorixation = Bearer access_code
在Body我有:
file = test.txt (this is file to be uploaded,able to select file location in Postman)
上面的内容在邮递员工具中按预期工作。
现在我使用 HttpClient 4.5.6 在 Java 中写了下面的代码作为
try {
CloseableHttpClient httpClient = HttpClients.createDefault();
File file = new File("C:\Users\lKadariya\Desktop\test.txt");
String authorizationValue = "Bearer "+accessToken;
HttpEntity data = MultipartEntityBuilder.create()
.setMode(HttpMultipartMode.BROWSER_COMPATIBLE)
.addBinaryBody("srcUploader",file,ContentType.DEFAULT_BINARY,file.getName())
.build();
HttpUriRequest request = RequestBuilder.post("https://transport-stg.transperfect.com/api/files/upload")
.addHeader("Content-Type","multipart/form-data")
.addHeader("Authorization",authorizationValue)
.addHeader("Accept","application/json")
.addParameter("filename","test.txt")
.addParameter("targetFolderId","2ff5ea6a-8187-4622-ab58-c7511c445ae7")
.setEntity(data)
.build();
ResponseHandler<String> responseHandler = response -> {
int status = response.getStatusLine().getStatusCode();
if (status >= 200 && status < 300) {
HttpEntity entity = response.getEntity();
return entity != null ? EntityUtils.toString(entity) : null;
} else {
throw new ClientProtocolException("Unexpected response status: " + status);
}
};
String responseBody = httpClient.execute(request, responseHandler);
而且我无法上传文件。
错误:无法从服务器上传文件。
邮递员给出了预期的回复。
上面的 Httpclient 代码可能出了什么问题?
OKhttp 库也工作正常,因为
OkHttpClient client = new OkHttpClient();
RequestBody requestBody = new MultipartBody.Builder()
.setType(MultipartBody.FORM)
// .addPart(
// Headers.of("Content-Disposition", "form-data; name=\"title\""),
// RequestBody.create(null, "test"))
.addPart(
Headers.of("Content-Disposition", "form-data; name=\"test\""),
RequestBody.create(MEDIA_TYPE_TXT, new File("test.txt")))
// .addFormDataPart("file","file.txt",
// RequestBody.create(MediaType.parse("application/octet-stream"),new File("test.txt"))
// )
.build();
Request request = new Request.Builder()
.header("Authorization", "Bearer " + accessToken)
.url("https://transport-stg.transperfect.com/api/files/upload?filename=basic.txt&targetFolderId=2ff5ea6a-8187-4622-ab58-c7511c445ae7")
.post(requestBody)
.header("Content-Type","multipart/form-data")
.build();
try {
Response response = client.newCall(request).execute();
HttpClient 代码会出现什么问题?
HttpUriRequest request = RequestBuilder.post("https://transport-stg.transperfect.com/api/files/upload")
.addHeader("Content-Type","multipart/form-data")
代码中的 Content-Type
值无效,因为它不包含 boundary
参数。
您不应该在请求级别干预 Content-Type
header 并让 HttpClient 根据包含的请求实体的属性自动生成它。
我正在尝试将文件上传到服务器。使用 Postman 工具时请求工作正常。
在 Postman 中我有:
POST :https://transport-stg.transperfect.com/api/files/upload?filename=test.png&targetFolderId=2ff5ea6a-8187-4622-ab58-c7511c445ae7
在 Header 我有 :
Content-Type = multipart/form-data
Authorixation = Bearer access_code
在Body我有:
file = test.txt (this is file to be uploaded,able to select file location in Postman)
上面的内容在邮递员工具中按预期工作。
现在我使用 HttpClient 4.5.6 在 Java 中写了下面的代码作为
try {
CloseableHttpClient httpClient = HttpClients.createDefault();
File file = new File("C:\Users\lKadariya\Desktop\test.txt");
String authorizationValue = "Bearer "+accessToken;
HttpEntity data = MultipartEntityBuilder.create()
.setMode(HttpMultipartMode.BROWSER_COMPATIBLE)
.addBinaryBody("srcUploader",file,ContentType.DEFAULT_BINARY,file.getName())
.build();
HttpUriRequest request = RequestBuilder.post("https://transport-stg.transperfect.com/api/files/upload")
.addHeader("Content-Type","multipart/form-data")
.addHeader("Authorization",authorizationValue)
.addHeader("Accept","application/json")
.addParameter("filename","test.txt")
.addParameter("targetFolderId","2ff5ea6a-8187-4622-ab58-c7511c445ae7")
.setEntity(data)
.build();
ResponseHandler<String> responseHandler = response -> {
int status = response.getStatusLine().getStatusCode();
if (status >= 200 && status < 300) {
HttpEntity entity = response.getEntity();
return entity != null ? EntityUtils.toString(entity) : null;
} else {
throw new ClientProtocolException("Unexpected response status: " + status);
}
};
String responseBody = httpClient.execute(request, responseHandler);
而且我无法上传文件。 错误:无法从服务器上传文件。 邮递员给出了预期的回复。
上面的 Httpclient 代码可能出了什么问题?
OKhttp 库也工作正常,因为
OkHttpClient client = new OkHttpClient();
RequestBody requestBody = new MultipartBody.Builder()
.setType(MultipartBody.FORM)
// .addPart(
// Headers.of("Content-Disposition", "form-data; name=\"title\""),
// RequestBody.create(null, "test"))
.addPart(
Headers.of("Content-Disposition", "form-data; name=\"test\""),
RequestBody.create(MEDIA_TYPE_TXT, new File("test.txt")))
// .addFormDataPart("file","file.txt",
// RequestBody.create(MediaType.parse("application/octet-stream"),new File("test.txt"))
// )
.build();
Request request = new Request.Builder()
.header("Authorization", "Bearer " + accessToken)
.url("https://transport-stg.transperfect.com/api/files/upload?filename=basic.txt&targetFolderId=2ff5ea6a-8187-4622-ab58-c7511c445ae7")
.post(requestBody)
.header("Content-Type","multipart/form-data")
.build();
try {
Response response = client.newCall(request).execute();
HttpClient 代码会出现什么问题?
HttpUriRequest request = RequestBuilder.post("https://transport-stg.transperfect.com/api/files/upload")
.addHeader("Content-Type","multipart/form-data")
代码中的 Content-Type
值无效,因为它不包含 boundary
参数。
您不应该在请求级别干预 Content-Type
header 并让 HttpClient 根据包含的请求实体的属性自动生成它。