问题描述
我尝试通过 UIWebView
访问需要Cookie的特定网址,但我无法访问它,因为Cookie已停用。所以我做了以下事情:
<$>
p> NSHTTPCookieStorage * cookieStorage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
[cookieStorage setCookieAcceptPolicy:NSHTTPCookieAcceptPolicyAlways];
创建 NSURLConnection
来自回应:
NSArray * cookies = [NSHTTPCookie cookiesWithResponseHeaderFields:[httpResponse allHeaderFields] forURL:response.URL];
[[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookies:cookies forURL:response.URL mainDocumentURL:nil];
但这两个都没有帮助。但是,如果我在safari中启动URL成功加载后,我可以加载相同的URL在 UIWebView
。
您可以帮我解决这个问题,如何为
UIWebView
创建 NSURLRequest
后,将sharedHTTPCookieStorage中的所有Cookie复制到 NSURLRequest
:
NSArray * cookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies];
NSMutableURLRequest * request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPShouldHandleCookies:YES];
[self addCookies:cookies forRequest:request];
[_webView loadRequest:request];
并添加 addCookies:forRequest
p>
- (void)addCookies:(NSArray *)cookies forRequest:(NSMutableURLRequest *)request
{
if ([cookies count]> 0)
{
NSHTTPCookie * cookie;
NSString * cookieHeader = nil;
for(cookie in cookie)
{
if(!cookieHeader)
{
cookieHeader = [NSString stringWithFormat:@%@ =%@,[ name],[cookie value]];
}
else
{
cookieHeader = [NSString stringWithFormat:@%@;%@ =%@,cookieHeader,[cookie name],[cookie value]];
}
}
if(cookieHeader)
{
[request setValue:cookieHeader forHTTPHeaderField:@Cookie];
}
}
}
I am trying to access a specific URL that requires cookies through UIWebView
but I can not access it because cookies are disabled. So I did the following:
Enabled cookies:
NSHTTPCookieStorage *cookieStorage = [NSHTTPCookieStorage sharedHTTPCookieStorage]; [cookieStorage setCookieAcceptPolicy:NSHTTPCookieAcceptPolicyAlways];
Created
NSURLConnection
and extracted cookies from response:NSArray *cookies = [ NSHTTPCookie cookiesWithResponseHeaderFields: [ httpResponse allHeaderFields ] forURL:response.URL]; [[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookies: cookies forURL: response.URL mainDocumentURL:nil];
But neither of this didn't help. However if I launch the URL in safari it loads successfully and after that I can load the same URL in UIWebView
too. Could you help me with this, how can I enable cookies for UIWebView
?
Thanks in advance
After create a NSURLRequest
, copy all cookies in sharedHTTPCookieStorage to NSURLRequest
:
NSArray *cookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPShouldHandleCookies:YES];
[self addCookies:cookies forRequest:request];
[_webView loadRequest:request];
And add addCookies:forRequest
method
- (void)addCookies:(NSArray *)cookies forRequest:(NSMutableURLRequest *)request
{
if ([cookies count] > 0)
{
NSHTTPCookie *cookie;
NSString *cookieHeader = nil;
for (cookie in cookies)
{
if (!cookieHeader)
{
cookieHeader = [NSString stringWithFormat: @"%@=%@",[cookie name],[cookie value]];
}
else
{
cookieHeader = [NSString stringWithFormat: @"%@; %@=%@",cookieHeader,[cookie name],[cookie value]];
}
}
if (cookieHeader)
{
[request setValue:cookieHeader forHTTPHeaderField:@"Cookie"];
}
}
}
这篇关于UIWebview启用Cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!