Web 服务未从 android 接收多部分 PUT

Webservice not receiving multipart PUT from android

我正在尝试通过 PUT 将 PDF 文件上传到 Web 服务。我通过多部分表单请求在 iOS 上实现了这一点。

然而,当我从 Android 执行相同的操作时,服务 returns 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];

如有任何想法,我们将不胜感激。或者也许指出我可以更好地调试它的方向?谢谢

我通过向多部分实体添加边界并将请求的内容类型更改为

解决了这个问题
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());
        }
    });