我正在尝试通过PUT将PDF文件上传到网络服务。我通过多部分表单请求在iOS上实现了这一点。
然而,当我从Android做同样的事情时,该服务立即返回200,并且从未实际获得整个PUT,我无法弄清楚原因。我尝试过多种不同的方式来构建这个请求,所有这些都有相同的结果。
我目前正在使用loopj AsyncHTTPClient库来发出请求,这是我的代码:
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
ByteArrayBody body = new ByteArrayBody(bytes, ContentType.create("application/pdf"), "file.pdf");
builder.addPart("",body);
HttpEntity form = builder.build();
AsyncHttpClient client = new AsyncHttpClient();
client.setBasicAuth(authUser(), authPass());
client.put(context, url, form, "application/pdf", new AsyncHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
Log.d(TAG,"SUCCESS: " + statusCode + " response: " + responseBody.length);
}
@Override
public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
Log.e(TAG,"FAIL: " + statusCode + " response: " + responseBody + " ERROR: " + error.getMessage());
}
});
作为参考,这是我在iOS上用来做同样请求的代码 - 成功。我正在使用AFNetworking。
NSString *authStr = [NSString stringWithFormat:@"%@:%@", [self authUser], [self authPass]];
NSString *authenticationValue = [[authStr dataUsingEncoding:NSUTF8StringEncoding] base64EncodedStringWithOptions:0];
NSMutableURLRequest *request = [[AFHTTPRequestSerializer serializer] multipartFormRequestWithMethod:@"PUT" URLString:url parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
[formData appendPartWithFileData:data
name:@""
fileName:@""
mimeType:mimeType];
} error:nil];
[request setValue:[NSString stringWithFormat:@"Basic %@", authenticationValue] forHTTPHeaderField:@"Authorization"];
AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
manager.responseSerializer = [AFJSONResponseSerializer serializer];
NSURLSessionUploadTask *uploadTask = [manager uploadTaskWithStreamedRequest:request progress:&progress completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *) response;
if(progress.cancelled) {
completion(nil, (int)httpResponse.statusCode, [NSError errorWithDomain:@"Network.uploadFile" code:500 userInfo:@{NSLocalizedDescriptionKey:@"User Cancelled"}]);
} else {
if (error) {
completion(nil, (int)httpResponse.statusCode, error);
} else {
NSDictionary *response = responseObject;
completion(response, (int)httpResponse.statusCode, nil);
}
}
}];
_currentUploadTask = uploadTask;
[uploadTask resume];
任何想法都表示赞赏。或者或许指出我可以更好地调试它的方向?感谢。
答案 0 :(得分:0)
我通过向多部分实体添加边界并将请求的内容类型更改为
来解决此问题multipart/form-data; boundary=
这是更新后的工作代码
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
String boundary = "-------------" + System.currentTimeMillis();
builder.setBoundary(boundary);
ByteArrayBody body = new ByteArrayBody(bytes, ContentType.create("application/pdf"), "file.pdf");
builder.addPart("",body);
HttpEntity form = builder.build();
AsyncHttpClient client = new AsyncHttpClient();
client.setBasicAuth(authUser(), authPass());
String type = "multipart/form-data; boundary="+boundary;
client.put(context, url, form, type, new AsyncHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
Log.d(TAG,"SUCCESS: " + statusCode + " response: " + responseBody.length + " headers: " + headers.toString());
}
@Override
public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
Log.e(TAG,"FAIL: " + statusCode + " response: " + responseBody + " ERROR: " + error.getMessage());
}
});