第一句:首先声明一个NSMutableURLRequest变量并把request拷贝过去。
第二句:调用addValue方法添加请求头就是了,forHTTPHeaderField是添加头域。
第三句,把NSMutableURLRequest变量拷贝回去。
NSString *strURL =[[NSString alloc]
initWithFormat:@"http://apis.baidu.com/heweather/weather/free?city=%@",
@"guangzhou"];
strURL = [strURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url = [NSURL URLWithString:strURL];
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];
NSMutableURLRequest *mutableRequest = [request mutableCopy];
[mutableRequest addValue:@"你的apikey" forHTTPHeaderField:@"apikey"];
request = [mutableRequest copy];
NSLog(@"%@", request.allHTTPHeaderFields);
NSURLConnection *connection = [[NSURLConnection alloc]
initWithRequest:request
delegate:self];