AFNetWorking 之 网络请求的基本知识

时间:2022-04-01 19:13:20
NSString *urlStr = @"http://api.openweathermap.org/data/2.5/forecast/daily";

    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];

    // 设置回复内容信息
manager.responseSerializer.acceptableContentTypes = [manager.responseSerializer.acceptableContentTypes setByAddingObject:@"text/html"]; // 请求的方法
[manager GET:urlStr
parameters:@{@"lat" : @"39.907501",
@"lon" : @"116.397232",
@"cnt" : @""} // 请求的正文
success:^(AFHTTPRequestOperation *operation, id responseObject) { // 请求头部信息(我们执行网络请求的时候给服务器发送的包头信息)
NSLog(@"%@", operation.request.allHTTPHeaderFields); // 服务器给我们返回的包得头部信息
NSLog(@"%@", operation.response); // 返回的数据
NSLog(@"%@", responseObject);
}
failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"%@", error);
}];

1. 请求类

  AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];

2. Request Field

  请求的头部信息:AFHTTPRequestOperation operation.request.allHTTPHeaderFields

3. Response

  operation.response

4. 设置收到的信息格式

  manager.responseSerializer.acceptableContentTypes = [manager.responseSerializer.acceptableContentTypes setByAddingObject:@"text/html"]