AFN3.0 Patch Request Error Error Domain=nsurlerrordomain code=-1012 "(null)"
Last Update:2018-07-26
Source: Internet
Author: User
I tried to execute an HTTP PATCH request, but I keep getting error domain = nsurlerrordomaincode = 1012 error. My code:
// Encapsulated request method
+ (void) requestMethod: (NSString *) method urlStr: (NSString *) urlStr token: (NSString *) token parma: (NSDictionary *) param success: (void (^) (id)) success failure: (void (^ ) (NSError *)) failure {
NSMutableURLRequest * request = [[AFHTTPRequestSerializer serializer] requestWithMethod: method URLString: urlStr parameters: param error: nil];
[request setValue: token forHTTPHeaderField: @ "Authorization"];
NSOperationQueue * queue = [[NSOperationQueue alloc] init];
[NSURLConnection sendAsynchronousRequest: request queue: queue completionHandler: ^ (NSURLResponse * response, NSData * data, NSError * connectionError) {
if (! connectionError) {
NSDictionary * dict = [NSJSONSerialization JSONObjectWithData: data options: 0 error: nil];
success (dict);
} else {
failure (connectionError);
}
}];
}
// upload
NSString * dicStr = [self setDictionaryToString: dict];
NSString * urlStr = [NSString stringWithFormat: @ "http://123.56.77.171/app/user/user/modifyphone/oldcaptcha?%@", dicStr];
NSString * newStr = [urlStr stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding];
NSString * value = [NSString stringWithFormat: @ "% @", [defaults objectForKey: @ "userToken"]];
[HttpHelper requestMethod: @ "PATCH" urlStr: newStr token: value parma: nil success: ^ (id json) {
NSLog (@ "% @", json);
} failure: ^ (NSError * error) {
NSLog (@ "% @", error);
}];
Convert the requested parameters from a dictionary into a string and stitch it into a URL string
Result / log, I get: Response code
Error Domain = NSURLErrorDomain Code = -1012 "(null)" UserInfo = {NSErrorFailingURLStringKey = http: //123.56.77.171/app/user/user/modifyphone/oldcaptcha, NSUnderlyingError = 0x7fd768c882e0 {Error Domain = kCFErrorDomainCFNetwork Code = -1012 "( null) "UserInfo = {_ kCFURLErrorAuthFailedResponseKey = <CFURLResponse 0x7fd76b08c300 [0x10d2117b0]> {url = http://123.56.77.171/app/user/user/modifyphone/oldcaptcha}}}, NSErrorFailingURLKey = http: //123.56.77.171/app / user / user / modifyphone / oldcaptcha}
Solution Replace the following code with
AFN3.0 PATCH request error Error Domain = NSURLErrorDomain Code = -1012 '(null)'
NSString * dicStr = [self setDictionaryToString: dict];
NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
NSString * urlStr = [NSString stringWithFormat: @ "http://123.56.77.171/app/user/user/modifyphone/oldcaptcha?%@", dicStr];
NSString * newStr = [urlStr stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding];
NSString * value = [NSString stringWithFormat: @ "% @", [defaults objectForKey: @ "userToken"]];
NSMutableURLRequest * request = [[AFHTTPRequestSerializer serializer] requestWithMethod: method URLString: urlStr parameters: param error: nil];
NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
NSString * value = [NSString stringWithFormat: @ "% @", [defaults objectForKey: @ "userToken"]];
** AFURLSessionManager * manager = [[AFURLSessionManager alloc] initWithSessionConfiguration: [NSURLSessionConfiguration defaultSessionConfiguration]];
// This line of code is the point
[manager.securityPolicy setAllowInvalidCertificates: YES]; **
[request setValue: value forHTTPHeaderField: @ "Authorization"];
NSURLSessionUploadTask * uploadTask = [manager uploadTaskWithStreamedRequest: request progress: ^ (NSProgress * uploadProgress) {
} completionHandler: ^ (NSURLResponse * response, id responseObject, NSError * error) {
if (! error) {
success (responseObject);
} else {
failure (error);
}
}];
[uploadTask resume];