AFN3.0 Patch Request Error Error Domain=nsurlerrordomain code=-1012 "(null)"

Source: Internet
Author: User
Tags json response code
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];

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.