1

我的问题如下:

在整个应用程序中使用库 Afnetworking effettura 向我的网络服务器发布请求。Dopo 本人通过对服务器的特殊发布请求进行身份验证,我在请求中获取 cookie,并且库 Afnetworking 自动将其保存在 sharedHTTPCookieStorage 中。

由于 webview 不会自动使用这些 cookie,因此我编写了此方法以将 cookie 与先前找到的请求相关联:

NSHTTPCookie *cookie;
        NSHTTPCookieStorage *storage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
        for (cookie in [storage cookies])
        {
            NSString* domainName = [cookie domain];
            NSRange domainRange = [domainName rangeOfString:@"mydomain"];
            if(domainRange.length > 0)
            {  NSLog(@"Cookie find for domain url");
                NSLog(@"COOKIE.VALUE = %@",cookie.value);
                NSLog(@"COOKIE = %@",cookie);
                NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"my_url"]];
                //URL Requst Object

                NSMutableURLRequest *requestObj = [NSMutableURLRequest requestWithURL:url];
               [requestObj addValue:[NSString stringWithFormat:@"%@",cookie] forHTTPHeaderField:@"Cookie"];

                return requestObj;

然后 [self.webView loadRequest:requestObj];

但似乎服务器不处理任何cookie。

我还需要在对 webview 的请求中使用 cookie,该 cookie 之前通过使用库 afnetworking 发出的 post 请求登录。

提前致谢。

4

0 回答 0