使用NSURLConnetion类的NSURLConnectionDataDelegate中的代理方法
- (nullable NSURLRequest *)connection:(NSURLConnection *)connection willSendRequest:(NSURLRequest *)request redirectResponse:(nullable NSURLResponse *)response
创建NSURLConnection对象发Request
NSURL *url = [NSURL URLWithString:@"http://c.snnd.co/api/v4/click?campaign_id=4410534&publisher_id=52&rt=170401034908&_po=4f6801b69d272e96bbb920659a2f805b&_mw=p&_c=972&_cw=c&_ad=1288&publisher_slot=260&sub_1=pst2977485&pub_gaid=&pub_idfa=ADB6E76C-6B91-455B-81BE-DB884CC17818&pub_aid=&sub_2=695-669-773_33_0.300_20131822574410534260_iOS_1491019272&sub_3=Y29tLmdtZWRhbC5nb2xkZ21lZGFsKG5hdDMp_US_hcvr.ecpm5_i-1.5.3___ADB6E76C-6B91-455B-81BE-DB884CC17818&ch=pst"];
NSMutableURLRequest *quest = [NSMutableURLRequest requestWithURL:url];
quest.HTTPMethod = @"GET";
NSURLConnection *connect = [NSURLConnection connectionWithRequest:quest delegate:self];
[connect start];
- (nullable NSURLRequest *)connection:(NSURLConnection *)connection willSendRequest:(NSURLRequest *)request redirectResponse:(nullable NSURLResponse *)response{
NSHTTPURLResponse *urlResponse = (NSHTTPURLResponse *)response;
NSLog(@"%ld",urlResponse.statusCode);
NSLog(@"%@",urlResponse.allHeaderFields);
NSDictionary *dic = urlResponse.allHeaderFields;
NSLog(@"%@",dic[@"Location"]);
return request;
}
控制台输出如下:request 是你要发送的请求 urlResponse.statusCode 则是请求的状态码,302则是重定向 。
urlResponse.allHeaderFields中保存着http的头信息:
2017-04-01 14:14:13.923 URL302Demo[16338:587888] 302
2017-04-01 14:14:13.924 URL302Demo[16338:587888] {
"Cache-Control" = "no-store, no-cache, must-revalidate, max-age=0";
Connection = "keep-alive";
"Content-Type" = "text/html; charset=UTF-8";
Date = "Sat, 01 Apr 2017 06:14:13 GMT";
Expires = "Thu, 01 Jan 1970 00:00:00 GMT";
Location = "https://itunes.apple.com/us/app/star-wars-galaxy-of-heroes/id921022358";
Pragma = "no-cache";
Server = nginx;
"Transfer-Encoding" = Identity;
}
2017-04-01 14:14:13.925 URL302Demo[16338:587888] https://itunes.apple.com/us/app/star-wars-galaxy-of-heroes/id921022358