开发者社区> 问答> 正文

iOS WebView 中的 Cookie场景IP直连的几种方法(4)


iOS11 之前的处理方法:NSURLProtocal拦截后,手动管理 Cookie 的存储:


步骤:做 IP 替换时将原始 URL 保存到 Header 中

  1. + (NSURLRequest *)canonicalRequestForRequest:(NSURLRequest *)request {
  2.     NSMutableURLRequest *mutableReq = [request mutableCopy];
  3.     NSString *originalUrl = mutableReq.URL.absoluteString;
  4.     NSURL *url = [NSURL URLWithString:originalUrl];
  5.     // 异步接口获取IP地址
  6.     NSString *ip = [[HttpDnsService sharedInstance] getIpByHostAsync:url.host];
  7.     if (ip) {
  8.         NSRange hostFirstRange = [originalUrl rangeOfString:url.host];
  9.         if (NSNotFound != hostFirstRange.location) {
  10.             NSString *newUrl = [originalUrl stringByReplacingCharactersInRange:hostFirstRange withString:ip];
  11.             mutableReq.URL = [NSURL URLWithString:newUrl];
  12.             [mutableReq setValue:url.host forHTTPHeaderField:@"host"];
  13.             // 添加originalUrl保存原始URL
  14.             [mutableReq addValue:originalUrl forHTTPHeaderField:@"originalUrl"];
  15.         }
  16.     }
  17.     NSURLRequest *postRequestIncludeBody = [mutableReq cyl_getPostRequestIncludeBody];
  18.     return postRequestIncludeBody;
  19. }

然后获取到数据后,手动管理 Cookie:
  1. - (void)handleCookiesFromResponse:(NSURLResponse *)response {
  2.     NSString *originalURLString = [self.request valueForHTTPHeaderField:@"originalUrl"];
  3.     if ([response isKindOfClass:[NSHTTPURLResponse class]]) {
  4.         NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
  5.         NSDictionary<NSString *, NSString *> *allHeaderFields = httpResponse.allHeaderFields;
  6.         if (originalURLString && originalURLString.length > 0) {
  7.             NSArray *cookies = [NSHTTPCookie cookiesWithResponseHeaderFields:allHeaderFields forURL: [[NSURL alloc] initWithString:originalURLString]];
  8.             if (cookies && cookies.count > 0) {
  9.                 NSURL *originalURL = [NSURL URLWithString:originalURLString];
  10.                 [[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookies:cookies forURL:originalURL mainDocumentURL:nil];
  11.             }
  12.         }
  13.     }
  14. }
  15. - (void)task:(CFHTTPDNSRequestTask *)task didReceiveResponse:(NSURLResponse *)response cachePolicy:(NSURLCacheStoragePolicy)cachePolicy {
  16.     [self handleCookiesFromResponse:response];
  17.     [self.client URLProtocol:self didReceiveResponse:response cacheStoragePolicy:cachePolicy];
  18. }

发送请求前,向请求中添加Cookie信息:
  1. + (void)handleCookieWithRequest:(NSMutableURLRequest *)request {
  2.     NSString* originalURLString = [request valueForHTTPHeaderField:@"originalUrl"];
  3.     if (!originalURLString || originalURLString.length == 0) {
  4.         return;
  5.     }
  6.     NSArray *cookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies];
  7.     if (cookies && cookies.count >0) {
  8.         NSDictionary *cookieHeaders = [NSHTTPCookie requestHeaderFieldsWithCookies:cookies];
  9.         NSString *cookieString = [cookieHeaders objectForKey:@"Cookie"];
  10.         [request addValue:cookieString forHTTPHeaderField:@"Cookie"];
  11.     }
  12. }
  13. + (NSURLRequest *)canonicalRequestForRequest:(NSURLRequest *)request {
  14.     NSMutableURLRequest *mutableReq = [request mutableCopy];
  15. //...
  16.      [self handleCookieWithRequest:mutableReq];
  17.     return [mutableReq copy];
  18. }

展开
收起
猫饭先生 2017-10-20 11:21:42 1918 0
0 条回答
写回答
取消 提交回答
问答排行榜
最热
最新

相关电子书

更多
手淘iOS性能优化探索 立即下载
From Java/Android to Swift iOS 立即下载
深入剖析iOS性能优化 立即下载