
获取 Cookie 之前,APP 已经与服务器交互过,已经存在 Cookie 才行
网络请求添加Cookie都需要在request的时候添加
1 2 3 4 5 6 7 8 9 10 11 12 13
|
- (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增加请求头
1 2 3
|
NSMutableURLRequest *myRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:self.myAttachUrl]]; NSString *cookie = [self getCurrentCookie]; [myRequest addValue:cookie forHTTPHeaderField:@"Cookie"];
|
WKWebView加载请求
1
|
[self.myWKWebView loadRequest:self.myRequest];
|
近期评论