1
我想通過PUT將PDF文件上傳到Web服務。我通過多部分表單請求在iOS上實現此目標。Web服務沒有從android中接收多部分PUT
但是,當我從Android執行相同操作時,服務立即返回200,並且從未真正獲取完整的PUT,我無法弄清楚原因。我嘗試過多種不同的方式來構建這個請求,所有的都有相同的結果。
我目前使用的循環J 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];
任何想法都不勝感激。或者,也許可以將我指向一個我可以更好地調試的方向?謝謝。