我有一个包含3个部分的多部分请求,每个部分包含自己的主体和标题,所以我想要的是在其中一个部分上添加自定义标题。我试图通过改造和okhttp建立请求,但他们没有提供这样做的选项。有什么想法吗?
编辑:这是基于此question
答案 0 :(得分:0)
请尝试以下代码:
public HttpResponse multiPartRequest(String url, String token, File file) {
client = new DefaultHttpClient();
HttpPost request = new HttpPost(url);
HttpResponse response = null;
DRPContentForUpload content = new DRPContentForUpload(file);
String jsonObject = DRPJSONConverter.toJson(content);
String BOUNDARY= "--eriksboundry--";
request.setHeader("Content-Type", "multipart/form-data; boundary="+BOUNDARY);
request.addHeader("X-AUTHORIZATION",token);
MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE,BOUNDARY,Charset.defaultCharset());
try {
entity.addPart("file01", new StringBody(jsonObject));
entity.addPart("file01", new FileBody(file));
request.addHeader("Accept-Encoding", "gzip, deflate");
} catch (UnsupportedEncodingException e) {
Log.v("encoding exception","E::: "+e);
e.printStackTrace();
}
request.setHeader("Accept", "application/json");
request.setHeader("Content-Type", "multipart/form-data; boundary="+BOUNDARY);
request.setEntity(entity);
try {
response = client.execute(request);
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return response;
}