我正在尝试将图像从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];
我在一段时间后收到**请求超时错误**。它甚至没有到达服务器端平台。请告诉我该怎么做。谢谢......
答案 0 :(得分:0)
在BaseURL的末尾添加/
,使它看起来如下所示
AFHTTPClient *client= [AFHTTPClient clientWithBaseURL:[NSURL URLWithString:@"http://192.168.43.41:7001/"]];
并从下方代码中的网址中移除/
,看看它是否有效
NSMutableURLRequest *request1 = [client multipartFormRequestWithMethod:@"POST" path:@"Biometric/rest/face/face8" parameters:nil constructingBodyWithBlock: ^(id <AFMultipartFormData>formData)