• 使用 AFNetworking的时候,怎样管理 session ID


    问:

    As the title implies, I am using AFNetworking in an iOS project in which the application talks to a server. When the user signs in, the server responds by sending back a success flag and the response headers contain the session ID.

    I am wondering if AFNetworking automatically sends the session ID with every subsequent request or should I take care of this myself in some way?

    For your information, I have no control over the back-end in terms of how requests are authenticated. I am only building a client that talks to the server.



    答:

    Yes, your session ID should be sent automatically once you are logged in, as long as the cookie does not expire before the next request is sent (important detail to be sure of).NSURLConnection, which AFNetworking uses, takes care of the details for this for you.

    On the backend AFNetworking is using NSURLConnection which in turn automatically updatesNSHTTPCookieStorage to store the session. You can manipulate or delete the cookies as you see fit by messing with the cookie storage.

    Like if you wanted to appear to the service as not logged in, you could just delete the session cookie associated to that domain. Some services I have worked with will error if you are already logged in and attempt to login again. Additionally there was no way to check login status. Quick fix, get the cookies from URL and delete them :

    NSArray *cookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookiesForURL: networkServerAddress];
    for (NSHTTPCookie *cookie in cookies) 
    {
        [[NSHTTPCookieStorage sharedHTTPCookieStorage] deleteCookie:cookie];
    }
    


  • 相关阅读:
    软件仓库配置及编译http2.4及文件系统创建实例
    查找、打包、sed实例
    文件管理工具和基础脚本实例
    文件管理实例
    Linux系统中vim设置tab缩进为4个字符
    linux文件管理类命令及实例讲解
    文件元数据信息介绍及修改文件时间
    Linux发行版的系统目录名称命名规则以及用途
    回调和递归
    关于for循环的小案例
  • 原文地址:https://www.cnblogs.com/claireyuancy/p/6888597.html
Copyright © 2020-2023  润新知