iOS 网络编程 NSURLSessionDataTask 添加cookie

本文展示了一个使用 iOS 应用程序通过 NSURLSession 发起 HTTP GET 请求获取文章列表的具体实现方式,并展示了如何设置请求头、处理响应数据及解析 JSON 格式的数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

- (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=com.xiangha.XiangHaBake";

//    request.HTTPBody=[str dataUsingEncoding:NSUTF8StringEncoding];

    [request addValue:@"device=ios#iPhone 6 (A1549/A1586)#8.4#1.0.0#750#1334#appStore#Wifi#1086.87#com.xiangha.XiangHaBake#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];

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值