0

我正在尝试将图像从我的 iOS 本机应用程序上传到作为多部分/表单数据请求的 Restful Web 服务。为此,我正在使用 AFNetworking 库。

这部分的 HTML 版本如下所示:

<form action="rest/face/face8" method="post"
enctype="multipart/form-data">

UserId: <input type="text" name="userId"
value="cced82c0-513f-4125-95ac-ac37b3b58ee6"><br>

Enrollment Id: <input type="text" name="enrollmentId"
value="c7d8466a-fe55-454e-aad7-964f13696899"><br>

LicenseId: <input type="text" name="licenseId"
value="98a847ed-4148-4024-8265-6d3748468c3d"><br>

ClientVersion: <input type="text" name="clientVersion"
value="sdk 3.3.4 - iPhone 5 (GSM) - iOS 7.0.2"><br>

Select a file : <input type="file" name="samples" size="45" /><br>

<input type="submit" value="Upload It" />

我正在尝试在 iOS 应用程序中复制它。我的 Objective-C 代码如下所示:

UIImage *image= [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"IMG_0095" ofType:@"jpg"]];
NSData *photoData= UIImageJPEGRepresentation(image, 0.8);

AFHTTPClient *client= [AFHTTPClient clientWithBaseURL:[NSURL URLWithString:@"http://192.168.43.41:7001"]];
[client setDefaultHeader:@"multipart/form-data; charset=utf-8; boundary=0xKhTmLbOuNdArY" value:@"Content-Type"];
[client setDefaultHeader:@"close" value:@"Proxy-Connection"];
[client setDefaultHeader:@"close" value:@"Connection"];
//[client setDefaultHeader:[NSString stringWithFormat:@"%d", [photoData length]]  value:@"Content-Length"];
[client setDefaultHeader:@"Mobile 2.1.0 (iPhone; iPhone OS 6.1.3; en_US)" value:@"User-Agent"];


NSMutableURLRequest *request1 = [client multipartFormRequestWithMethod:@"POST" path:@"/Biometric/rest/face/face8" parameters:nil constructingBodyWithBlock: ^(id <AFMultipartFormData>formData) {
    [formData appendPartWithFormData:[[NSString stringWithFormat:@"cced82c0-513f-4125-95ac-ac37b3b58ee6"] dataUsingEncoding:NSUTF8StringEncoding] name:@"userId"];
    [formData appendPartWithFormData:[[NSString stringWithFormat:@"c7d8466a-fe55-454e-aad7-964f13696899"] dataUsingEncoding:NSUTF8StringEncoding] name:@"enrollmentId"];
    [formData appendPartWithFormData:[[NSString stringWithFormat:@"98a847ed-4148-4024-8265-6d3748468c3d"] dataUsingEncoding:NSUTF8StringEncoding] name:@"licenseId"];
    [formData appendPartWithFormData:[[NSString stringWithFormat:@"sdk 6.0.0 - iPhone 4s (GSM) - iOS 6.1.3"] dataUsingEncoding:NSUTF8StringEncoding] name:@"clientVersion"];
    [formData appendPartWithFileData:photoData name:@"samples" fileName:@"file" mimeType:@"application/octet-stream"];
}];

MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
hud.mode = MBProgressHUDModeDeterminate;
hud.labelText = @"Connecting to Middleware...";

[request1 setTimeoutInterval:180];

AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request1];

[operation setUploadProgressBlock:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) {
    NSLog(@"Sent %lld of %lld bytes", totalBytesWritten, totalBytesExpectedToWrite);
    float progress = totalBytesWritten / (float)totalBytesExpectedToWrite;
    hud.progress= progress;
}];

[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject)
 {
     [MBProgressHUD hideHUDForView:self.view animated:YES];
     NSDictionary *jsons = [NSJSONSerialization JSONObjectWithData:responseObject options:kNilOptions error:nil];
     NSLog(@"response headers: %@", [[operation response] allHeaderFields]);
     NSLog(@"response: %@",jsons);

 }
                                 failure:^(AFHTTPRequestOperation *operation, NSError *error)
 {
     [MBProgressHUD hideHUDForView:self.view animated:YES];
     if([operation.response statusCode] == 403)
     {
         NSLog(@"Upload Failed");
         return;
     }
     NSLog(@"error: %@", [error debugDescription]);

 }];

[operation start];

一段时间后我收到**请求超时错误**。它甚至没有到达服务器端平台。请告诉我我应该在这里做什么。谢谢 ...

4

1 回答 1

0

添加/在 BaseURL 的末尾,使其如下所示

AFHTTPClient *client= [AFHTTPClient clientWithBaseURL:[NSURL URLWithString:@"http://192.168.43.41:7001/"]];

/从下面代码中的 url 中删除,看看它是否有效

NSMutableURLRequest *request1 = [client multipartFormRequestWithMethod:@"POST" path:@"Biometric/rest/face/face8" parameters:nil constructingBodyWithBlock: ^(id <AFMultipartFormData>formData)
于 2013-11-08T17:41:00.953 回答