我需要发送cookie才能登录UIWebView,但是当我加载网页时,还没有发送cookie。
我的代码是:
CorreoViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
self.title =AMLocalizedString(@"Correo", @"");
NSURL *url=[NSURL URLWithString:@"https://cuentas.uv.es/cgi-bin/p/user/Usuario"];
NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:url];
NSArray * cookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies];
NSDictionary * headers = [NSHTTPCookie requestHeaderFieldsWithCookies: cookies];
[request setHTTPMethod:@"Post"];
[request setHTTPShouldHandleCookies:YES];
[request setAllHTTPHeaderFields:headers];
NSLog(@"Request: %@", [[request allHTTPHeaderFields] objectForKey:@"cookie"]);
[self.webView loadRequest:request];
// Do any additional setup after loading the view.
}
我在cookie存储中的cookie是:
COOKIE 0:NSHTTPCookie版本:0名称:“PAPIAuthNcook” 值: “nBNqHJSdjgo1aBw0uGh3AXzElIHCxA%2FDbnipSHWZbU5Eh1cJfUr18GEP9zxB%2BL%2FZGPBAKQu6oF2l%0AzIMt7ywiSECLBuOHJnvS7y6rVim2HfE%3D%0A” expiresDate:(null)创建时间:2014-01-16 15:05:41 +0000(4.11578e + 08) sessionOnly:TRUE域:“cuentas.uv.es”路径:“/”isSecure:FALSE
COOKIE 1:NSHTTPCookie版本:0名称:“PAPIslist” 值:“17201128679244079 | https://uvapp.uv.es/”expiresDate:(null) 创建时间:2014-01-16 15:05:41 +0000(4.11578e + 08)sessionOnly:TRUE domain:“cuentas.uv.es”path:“/”isSecure:FALSE
COOKIE 2:NSHTTPCookie版本:0名称:“PAPIAuthNcook” 值: “nBNqHJSdjgo1aBw0uGh3AXzElIHCxA%2FDbnipSHWZbU5Eh1cJfUr18GEP9zxB%2BL%2FZGPBAKQu6oF2l%0AzIMt7ywiSECLBuOHJnvS7y6rVim2HfE%3D%0A” expiresDate:(null)创建时间:2014-01-16 15:05:41 +0000(4.11578e + 08) sessionOnly:TRUE域:“as.uv.es”路径:“/”isSecure:FALSE
COOKIE 3:NSHTTPCookie版本:0名称:“PAPIslist” 值:“17201128679244079 | https://uvapp.uv.es/”expiresDate:(null) 创建时间:2014-01-16 15:05:41 +0000(4.11578e + 08)sessionOnly:TRUE domain:“as.uv.es”path:“/”isSecure:FALSE
AppDelegate.m
- (void)applicationDidBecomeActive:(UIApplication *)application
{
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
[[NSHTTPCookieStorage sharedHTTPCookieStorage]setCookieAcceptPolicy:NSHTTPCookieAcceptPolicyAlways];
}
提前谢谢。
答案 0 :(得分:4)
注释这一行:
NSArray * cookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies];
NSDictionary * headers = [NSHTTPCookie requestHeaderFieldsWithCookies: cookies];
[request setAllHTTPHeaderFields:headers];
自动发送cookie。我希望这能为你服务。