Handle HTTP error with NSURLSession?

前端 未结 5 1246
挽巷
挽巷 2021-01-07 16:33

I\'m trying to send a HTTP request with NSURLSession. It works fine, but when the server doesn\'t respond I can\'t find where the HTTP error code is stored. The

相关标签:
5条回答
  • 2021-01-07 16:58

    Swift 3:

    // handle basic connectivity issues here
    guard error == nil else {
        print("Error: ", error!)
        return
    }
    
    // handle HTTP errors here
    if let httpResponse = response as? HTTPURLResponse {
        let statusCode = httpResponse.statusCode
    
        if (statusCode != 200) {
            print ("dataTaskWithRequest HTTP status code:", statusCode)
            return;
        } 
    }
    
    if let data = data {
        // here, everything is probably fine and you should interpret the `data` contents
    }
    
    0 讨论(0)
  • 2021-01-07 17:03

    you could try something like this. I've created a simple method that will be able to post a data into server and get the server response. You can get the server status code via NSHTTPURLResponse class. Hope will help :)

    -(void) POST:(NSURL *) url URLparameters:(NSString *) parameters success:(void (^)(NSURLSessionDataTask *  task, id   responseObject)) successHandler errorHandler:(void (^)(NSURLSessionDataTask *  task, NSError *  error)) errorHandler{
         requestBody = [[NSMutableURLRequest alloc]
                       initWithURL:url
                       cachePolicy: NSURLRequestUseProtocolCachePolicy
                       timeoutInterval:60.0];
        [requestBody setHTTPMethod:@"POST"];
        [requestBody setValue:@"application/json" forHTTPHeaderField:@"Accept"];
        [requestBody setHTTPBody:[NSData dataWithBytes:
                                  [parameters UTF8String]length:strlen([parameters UTF8String])]];
        NSURLSession *session = [NSURLSession sessionWithConfiguration: sessionConfiguration delegate: self delegateQueue: [NSOperationQueue mainQueue]];
    
        NSURLSessionDataTask *task = [session dataTaskWithRequest:requestBody completionHandler:
                                      ^(NSData *data, NSURLResponse *response, NSError *error) {
                                          NSHTTPURLResponse* respHttp = (NSHTTPURLResponse*) response;
    
                                          if (respHttp.statusCode == SUCCESS) {
                                              NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
                                              successHandler(task, dictionary);
                                              NSLog(@"HTTP_STATUS: success %@", response);
                                          }else if (respHttp.statusCode == UNAUTHORIZE) {
                                              NSLog(@"HTTP_STATUS: anauthorize");
                                          }else if (respHttp.statusCode== BAD_REQUEST) {
                                              NSLog(@"HTTP_STATUS: badrequest");
                                          }else if (respHttp.statusCode == INTERNAL_SERVER_ERROR) {
                                              NSLog(@"HTTP_STATUS: internalerror");
                                          }else if (respHttp.statusCode== NOT_FOUND) {
                                              NSLog(@"HTTP_STATUS: internalerror");
                                          }
                                          errorHandler(task, error);
                                          return;
                                      }];
        [task resume];
    }
    
    0 讨论(0)
  • 2021-01-07 17:10

    The second parameter of the completionHandler is the NSURLResponse, which when doing a HTTP request, is generally a NSHTTPURLResponse. So, you'd generally do something like:

    NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:[self postRequestWithURLString:apiEntry parameters:parameters] completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
    
        // handle basic connectivity issues here
    
        if (error) {
            NSLog(@"dataTaskWithRequest error: %@", error);
            return;
        }
    
        // handle HTTP errors here
    
        if ([response isKindOfClass:[NSHTTPURLResponse class]]) {
    
            NSInteger statusCode = [(NSHTTPURLResponse *)response statusCode];
    
            if (statusCode != 200) {
                NSLog(@"dataTaskWithRequest HTTP status code: %d", statusCode);
                return;
            }
        }
    
        // otherwise, everything is probably fine and you should interpret the `data` contents
    
        NSLog(@"data: %@", [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);
    }];
    [dataTask resume];
    
    0 讨论(0)
  • 2021-01-07 17:13

    You can have the entire headers in allHeaderFields

    let realResponse = response as? NSHTTPURLResponse 
    realResponse.allHeaderFields
    

    kindly convert it to objective-C.

    0 讨论(0)
  • 2021-01-07 17:20

    If server-side error occurred data parameter from completion handler may contain some useful info

    In general I thin you should implement URLSession:task:didCompleteWithError: from NSURLSessionTaskDelegate protocol in session delegate

    docs: NSURLSessionTaskDelegate Protocol Reference

    0 讨论(0)
提交回复
热议问题