Html 在StringWithContents之后,URL cookies不会保存在UIWebView中。网间网操作系统
我想从url获取html,我使用:Html 在StringWithContents之后,URL cookies不会保存在UIWebView中。网间网操作系统,html,ios,cookies,save,nsurlconnectiondelegate,Html,Ios,Cookies,Save,Nsurlconnectiondelegate,我想从url获取html,我使用: NSString *URLtoHTML = [NSString stringWithContentsOfURL:self.url encoding:NSUTF8StringEncoding error:nil]; [self.webView loadHTMLString:URLtoHTML baseURL:nil]; 但在这之后,我的cookies会在UIWebView中清理。 但如果我使用load request而不使用StringWithContents
NSString *URLtoHTML = [NSString stringWithContentsOfURL:self.url encoding:NSUTF8StringEncoding error:nil];
[self.webView loadHTMLString:URLtoHTML baseURL:nil];
但在这之后,我的cookies会在UIWebView中清理。
但如果我使用load request而不使用StringWithContents URL cookies save:
[self.webView loadRequest:[NSURLRequest requestWithURL:self.url]];
我试过了,但没有保存:
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];
}
NSString *URLtoHTML = [NSString stringWithContentsOfURL:self.url encoding:NSUTF8StringEncoding error:nil];
[self.webView loadRequest:[NSURLRequest requestWithURL:self.url]];
如何获取HTML并将其与Cookie一起加载到UIWebView中
更新:
如果我使用这种情况(两条不相关的行),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];
}
NSString *URLtoHTML = [NSString stringWithContentsOfURL:self.url encoding:NSUTF8StringEncoding error:nil];
[self.webView loadRequest:[NSURLRequest requestWithURL:self.url]];
i、 e.
stringWithContentsOfURL
不保存cookies。这怎么可能?有趣的是:在这种情况下,您可以从URL下载原始内容,并在WebView中显示这些数据。WebView只是不知道你的cookies
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"];
我的解决方案: 首先,我检查了从服务器获取的设置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
}
我意识到我得到了价值,在这个价值中cookies被禁用。
现在,我首先发送启用cookies的请求:
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];
就这些!祝大家好运 但在这种情况下,我必须等待页面加载:-(void)webViewDidFinishLoad:(UIWebView*)webView{NSString*html=[self.webView stringByEvaluatingJavaScriptFromString:@“document.body.innerHTML”];}这是一个很长的时间,不适合我。有什么建议吗?