我想从网址获取HTML并使用:
NSString *URLtoHTML = [NSString stringWithContentsOfURL:self.url encoding:NSUTF8StringEncoding error:nil];
[self.webView loadHTMLString:URLtoHTML baseURL:nil];
但在此之后我的cookies在UIWebView中清理。 但如果我使用加载请求而没有stringWithContentsOfURL cookies保存:
[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];
}
如何获取HTML并使用Cookie在UIWebView中加载它?
更新 如果我使用这种情况(两个不相关的行)cookie也不能保存:
NSString *URLtoHTML = [NSString stringWithContentsOfURL:self.url encoding:NSUTF8StringEncoding error:nil];
[self.webView loadRequest:[NSURLRequest requestWithURL:self.url]];
即。 stringWithContentsOfURL
无法保存Cookie。怎么会这样?这很有趣:D
答案 0 :(得分:0)
在这种情况下 - 您从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"];
答案 1 :(得分:0)
我的解决方案:
首先,我检查了从服务器获取的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:
我发送带有帖子数据的请求
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];
这就是全部!祝大家好运!