在 stringWithContentsOfURL cookies 之后不保存在 UIWebView 中。 iOS
After stringWithContentsOfURL cookies not save in UIWebView. iOS
我想从 url 得到 html 并且我使用:
NSString *URLtoHTML = [NSString stringWithContentsOfURL:self.url encoding:NSUTF8StringEncoding error:nil];
[self.webView loadHTMLString:URLtoHTML baseURL:nil];
但在这之后我的 cookie 在 UIWebView 中清理。
但是如果我使用不带 stringWithContentsOfURL cookie 的加载请求保存:
[self.webView loadRequest:[NSURLRequest requestWithURL:self.url]];
我试过了,但是cookies也保存不了:
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[receivedData appendData:data];
NSString *dataString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
}
如何获取 HTML 并使用 cookie 在 UIWebView 中加载它?
更新:
如果我使用这种情况(两条不相关的行)cookie 也不会保存:
NSString *URLtoHTML = [NSString stringWithContentsOfURL:self.url encoding:NSUTF8StringEncoding error:nil];
[self.webView loadRequest:[NSURLRequest requestWithURL:self.url]];
即stringWithContentsOfURL
不保存 cookie。怎么会这样?这很有趣 :D
在这种情况下 - 您从 URL 下载原始内容,并仅在 WebView 中显示此数据。 WebView 只是不知道您的 cookie。
NSString *URLtoHTML = [NSString stringWithContentsOfURL:self.url encoding:NSUTF8StringEncoding error:nil];
[self.webView loadHTMLString:URLtoHTML baseURL:nil];
在这种情况下 - 您创建连接,但再次只下载原始数据。
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[receivedData appendData:data];
NSString *dataString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
}
这个更接近,您只需要它 - 它是从 WebView 捕获加载的数据。
[self.webView loadRequest:[NSURLRequest requestWithURL:self.url]];
NSString *html = [self.webView stringByEvaluatingJavaScriptFromString:
@"document.body.innerHTML"];
我的解决方案:
首先我检查了从服务器获取的 Set-Cookie:
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.simple.com/"]];
NSURLConnection *connection = [NSURLConnection connectionWithRequest:request delegate:self];
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
NSHTTPURLResponse *HTTPResponse = (NSHTTPURLResponse *)response;
NSDictionary *fields = [HTTPResponse allHeaderFields];
NSString *cookie = [fields valueForKey:@"Set-Cookie"]; // It is cookie
}
我意识到我很有价值,其中禁用了 cookie。
现在我首先发送一个启用了 cookie 的请求:
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://www.simple.com/"]];
[request addValue:myCustomCookies forHTTPHeaderField:@"Cookie"];
我的解决方案 2:
我用 post 数据发送请求
NSString *post = [NSString stringWithFormat:@"login=%@&passwors=%@",_login.text,_password.text];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%lu",(unsigned long)[postData length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://www.simple.com/login"]]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];
request.timeoutInterval = 20;
NSURLConnection *conn = [[NSURLConnection alloc]initWithRequest:request delegate:self];
就是这样!祝大家好运!
我想从 url 得到 html 并且我使用:
NSString *URLtoHTML = [NSString stringWithContentsOfURL:self.url encoding:NSUTF8StringEncoding error:nil];
[self.webView loadHTMLString:URLtoHTML baseURL:nil];
但在这之后我的 cookie 在 UIWebView 中清理。 但是如果我使用不带 stringWithContentsOfURL cookie 的加载请求保存:
[self.webView loadRequest:[NSURLRequest requestWithURL:self.url]];
我试过了,但是cookies也保存不了:
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[receivedData appendData:data];
NSString *dataString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
}
如何获取 HTML 并使用 cookie 在 UIWebView 中加载它?
更新: 如果我使用这种情况(两条不相关的行)cookie 也不会保存:
NSString *URLtoHTML = [NSString stringWithContentsOfURL:self.url encoding:NSUTF8StringEncoding error:nil];
[self.webView loadRequest:[NSURLRequest requestWithURL:self.url]];
即stringWithContentsOfURL
不保存 cookie。怎么会这样?这很有趣 :D
在这种情况下 - 您从 URL 下载原始内容,并仅在 WebView 中显示此数据。 WebView 只是不知道您的 cookie。
NSString *URLtoHTML = [NSString stringWithContentsOfURL:self.url encoding:NSUTF8StringEncoding error:nil];
[self.webView loadHTMLString:URLtoHTML baseURL:nil];
在这种情况下 - 您创建连接,但再次只下载原始数据。
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[receivedData appendData:data];
NSString *dataString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
}
这个更接近,您只需要它 - 它是从 WebView 捕获加载的数据。
[self.webView loadRequest:[NSURLRequest requestWithURL:self.url]];
NSString *html = [self.webView stringByEvaluatingJavaScriptFromString:
@"document.body.innerHTML"];
我的解决方案:
首先我检查了从服务器获取的 Set-Cookie:
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.simple.com/"]];
NSURLConnection *connection = [NSURLConnection connectionWithRequest:request delegate:self];
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
NSHTTPURLResponse *HTTPResponse = (NSHTTPURLResponse *)response;
NSDictionary *fields = [HTTPResponse allHeaderFields];
NSString *cookie = [fields valueForKey:@"Set-Cookie"]; // It is cookie
}
我意识到我很有价值,其中禁用了 cookie。 现在我首先发送一个启用了 cookie 的请求:
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://www.simple.com/"]];
[request addValue:myCustomCookies forHTTPHeaderField:@"Cookie"];
我的解决方案 2:
我用 post 数据发送请求
NSString *post = [NSString stringWithFormat:@"login=%@&passwors=%@",_login.text,_password.text];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%lu",(unsigned long)[postData length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://www.simple.com/login"]]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];
request.timeoutInterval = 20;
NSURLConnection *conn = [[NSURLConnection alloc]initWithRequest:request delegate:self];
就是这样!祝大家好运!