iOS 网络编程 NSURLSessionDataTask 添加cookie

时间:2025-03-29 08:49:22

- (void)getArticelList

{

    NSString *strUrl=@"http://xxxx/category/so/getSoCaipu?s=%E9%A5%BC%E5%B9%B2&page=34";

    NSURL *url=[NSURL URLWithString:strUrl];

    

    NSMutableURLRequest *request=[[NSMutableURLRequest alloc] initWithURL:url];

//    [request setHTTPMethod:@"POST"];

    [request setHTTPShouldHandleCookies:YES];

//    NSString *str=@"name=";

//    =[str dataUsingEncoding:NSUTF8StringEncoding];

    [request addValue:@"device=ios#iPhone 6 (A1549/A1586)#8.4#1.0.0#750#1334#appStore#Wifi#1086.87##2#; User-Agent=xhapp#ios#1.0.0; userCode=; USERID=m2fogsb1gb7kldf27censtbq11; xhCode=BEB015940C8EE497094BDA6E056260DE" forHTTPHeaderField:@"Cookie"];

    

    

    [request addValue:@"XHBaking/1.0.0 (iPhone; iOS 8.4; Scale/2.00) Paros/3.2.13" forHTTPHeaderField:@"User-Agent"];

    

    NSURLSession *session=[NSURLSession sharedSession];

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

//        NSLog(@"----:%@",data);

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

//        NSLog(@"%@",dict);

        

        NSArray *arr1=dict[@"data"];

        

        

       

        for (NSDictionary *d in arr1) {

//            NSLog(@"%@",d);

            NSLog(@"%@",d[@"name"]);

        }

    }];

    [dataTask resume];

}