网络请求添加Cookie都需要在request的时候添加
//获取Cookie
- (NSString *)getCurrentCookie {
NSMutableString *cookieStr = [[NSMutableString alloc] init];
NSArray *array = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookiesForURL:[NSURL URLWithString:@"https://www.baidu.com"]];
if ([array count] > 0) {
for (NSHTTPCookie *cookie in array) {
[cookieStr appendFormat:@"%@=%@;",cookie.name,cookie.value];
}
//删除最后一个分号 “;”
[cookieStr deleteCharactersInRange:NSMakeRange(cookieString.length - 1, 1)];
}
return cookieStr;
}
给request增加请求头
NSMutableURLRequest *myRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:self.myAttachUrl]];
NSString *cookie = [self getCurrentCookie];
[myRequest addValue:cookie forHTTPHeaderField:@"Cookie"];
WKWebView加载请求
[self.myWKWebView loadRequest:self.myRequest];
网友评论