本文介绍了具有自签名SSL和HTTP Basic Auth的AFHTTPRequestOperation(-1012错误)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试通过 AFHTTPRequestOperation
将照片上传到我的网络服务器。这是我的 AFHTTPSessionManager
。通过此经理
的所有http请求都可以正常工作。
I am trying to upload photo to my web-server via AFHTTPRequestOperation
. Here is my AFHTTPSessionManager
. All http-requests via this manager
works perfect.
- (AFHTTPSessionManager *)manager
{
if (_manager == nil) {
_manager = [[AFHTTPSessionManager alloc] initWithBaseURL:[NSURL URLWithString:@"https://my.domain.com/api/v2"]];
[_manager.requestSerializer setAuthorizationHeaderFieldWithUsername:@"username" password:@"password"];
AFSecurityPolicy *policy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone];
policy.allowInvalidCertificates = YES;
_manager.securityPolicy = policy;
}
return _manager;
}
但是当我尝试上传带有进度处理的照片时:
But when I am trying to upload photo with progress handling:
- (void)photoSend:(UIImage *)photo
toUsers:(NSArray *)users
completion:(CompletionHandler)completion
progress:(ProgressionHandler)progress
{
NSDictionary *params = @{
@"token":self.token,
@"to_usernames":[users componentsJoinedByString:@","],
};
NSString *urlString = [[NSURL URLWithString:@"photo/send" relativeToURL:self.manager.baseURL] absoluteString];
NSMutableURLRequest *request = [self.manager.requestSerializer multipartFormRequestWithMethod:@"POST" URLString:urlString parameters:params constructingBodyWithBlock:^(id <AFMultipartFormData> formData)
{
[formData appendPartWithFileData:UIImageJPEGRepresentation(photo,0.8)
name:@"photo"
fileName:@"image.jpg"
mimeType:@"image/jpg"];
} error:nil];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
operation.responseSerializer = [AFJSONResponseSerializer serializer];
// Following 2 lines does not make sense. Http headers contains auth data without this code
//NSURLCredential *credential = [NSURLCredential credentialWithUser:@"login" password:@"password" persistence:NSURLCredentialPersistenceNone];
//[operation setCredential:credential];
[operation setUploadProgressBlock:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) {
progress(totalBytesWritten*1.0/totalBytesExpectedToWrite);
}];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
completion(operation.response, responseObject, operation.error);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
completion(operation.response, nil, error);
}];
[self.manager.operationQueue addOperation:operation];
}
我收到以下错误:
推荐答案
以下是解决方案:
AFSecurityPolicy *policy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone];
policy.allowInvalidCertificates = YES;
operation.securityPolicy = policy;
所以HTTP基本身份验证数据由经理提供给操作,但SSL策略 - 不是。
So HTTP basic auth data is provided to operation by manager, but SSL policy - not.
这篇关于具有自签名SSL和HTTP Basic Auth的AFHTTPRequestOperation(-1012错误)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!