美文网首页
用原生的网络请求

用原生的网络请求

作者: 洛奇丶 | 来源:发表于2018-01-11 15:42 被阅读0次

    1.

    2.

    复制:

    1.NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];

        NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration];

        NSURL *url = [NSURL URLWithString:@"https://pixabay.com/api/?key=4572819-33c1e1dcbac7521c915689a81&&image_type=photo"];

        NSURLRequest *repuest = [NSURLRequest requestWithURL:url];

        NSURLSessionDataTask *task = [session dataTaskWithRequest:repuest completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {

            NSDictionary *result = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];

            NSMutableArray *urlS = [NSMutableArray array];

            for (NSDictionary *dict in result[@"hits"]) {

                NSString *linkurl = dict[@"webformatURL"];

                [urlS addObject:linkurl];

            }

            _URLStrings = urlS;

            dispatch_async(dispatch_get_main_queue(), ^{

                [self.collectionView reloadData];

            });

        }];

        [task resume];

    2.

    - (void)lqPostRequestByServiceName:(NSString *)service

                              andAppi:(NSString *)api

                                parmas:(NSDictionary *)parmas

                            succeBlock:(void(^)(id _Nullable response))succeBlock

                            errorBlock:(void(^)(id _Nullable error))errorBlock{

        NSString *urlStr = [service stringByAppendingString:api];

        NSURL *url = [NSURL URLWithString:urlStr];

        NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];

        [request setHTTPMethod:@"POST"];

        NSArray *allKeysArr = [parmas allKeys];

        NSMutableString *result = [NSMutableString string];

        for (NSString *keyStr in allKeysArr) {

            NSString *str = [NSString stringWithFormat:@"%@=%@&",keyStr,parmas[keyStr]];

            [result appendString:str];

        }

        NSString *targetStr = [result substringWithRange:NSMakeRange(0,result.length - 1)];

        NSData *targetData = [targetStr dataUsingEncoding:NSUTF8StringEncoding];

        [request setHTTPBody:targetData];

        [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];

        [request setValue:[NSString stringWithFormat:@"%ld",targetData.length] forHTTPHeaderField:@"Content-Length"];

        request.timeoutInterval = 10;

        NSURLSession *session = [NSURLSession sharedSession];

        NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {

            NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];

            if (dic) {

                succeBlock(dic);

            }else{

                errorBlock(error.description);

            }

        }];

        [dataTask resume];

    }

    相关文章

      网友评论

          本文标题:用原生的网络请求

          本文链接:https://www.haomeiwen.com/subject/uyftoxtx.html