iOS: NSUrlConnection & SSL, Http connection frameworks

来源:互联网 发布:unity3d导入图片 编辑:程序博客网 时间:2024/06/01 15:13

java有一个httpclient open source lib来进行http connection操作,android就是使用它


iOS自身是通过NSUrlConnection来进行http connection。另外也有一些open source的http connection frameworks:

1. ASIHttpRequest (http://allseeing-i.com/ASIHTTPRequest/) 这个lib曾经是最多人使用的lib,但目前已经停止开发了。

asihttprequest的作者推荐了一些其他的framework,见 http://allseeing-i.com/[request_release];

2. AFNetworking (https://github.com/AFNetworking/AFNetworking)

3. LRResty (http://projects.lukeredpath.co.uk/resty/)   a lightweight lib

4. RestKit (http://restkit.org/)

以及被一些人认为是最有前途的MKNetworkKit framework

5. MKNetworkKit (http://blog.mugunthkumar.com/products/ios-framework-introducing-mknetworkkit/)



目前的project对http connection的requirement有2个issue:HTTPS 和 cookie/session maintain


About cookie/session, 一个典型的例子就是:user需要先login才能access其他的page。那么在android/ios app里,基本流程是:先send post request with user name and password param to login url来login,然后在client side保存cookies info,从而使得send GET request to access other web page不需要再跳到login page。

要实现这个功能,在使用java的httpclient lib时,是由HttpClient的实例来保存cookie data。也就是说当你用httpClient的实例A来send post request to login,那么之后你send get request,也必须由该实例A来send。而如果你重新创建另一个httpclient的实例B来send get request,那么就会再跳到login page。

而对于iOS的NSUrlConnection, 在你的app的生命周期里,iOS的loading url system会自动sends any stored cookies appropriate for an NSURLRequest。即当用NSUrlConnection来send post request之后,cookie会自动存储在iOS的loading url system中,当再次用NSUrlConnection send get request时,会自动在request中加入cookie info。注意:Cookies are not shared by applications in iOS.

上面这段话的原文为 (参考 https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/URLLoadingSystem/Concepts/URLOverview.html)

The URL loading system automatically sends any stored cookies appropriate for an NSURLRequest. unless the request specifies not to send cookies. Likewise, cookies returned in an NSURLResponse are accepted in accordance with the current cookie acceptance policy.

The NSHTTPCookieStorage class provides the interface for managing the collection of NSHTTPCookie objects shared by all applications.

iOS Note: Cookies are not shared by applications in iOS.




About HTTPS,无论是android 还是 iOS (目前在android simulator/device和iOS simulator测试过),使用httpclient/NSUrlConnection来send http request 还是 https request都没有什么分别。但是网上有说iOS app在send https request时,会出现ssl验证窗口 or throws ssl error,相关的解决方案见下列文档:

http://stackoverflow.com/questions/933331/how-to-use-nsurlconnection-to-connect-with-ssl-for-an-untrusted-cert

http://blog.csdn.net/favormm/article/details/6956588

http://iosdevelopersnote.blogspot.com/2011/05/nsurlconnection-over-https.html

http://www.cnblogs.com/visen-0/archive/2011/07/20/2111606.html (最后部分)





使用NSUrlConnection的参考文档有:

iOS的URL loading system的机制:https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/URLLoadingSystem/Concepts/URLOverview.html

NSUrlConnection官方例子: https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/URLLoadingSystem/Tasks/UsingNSURLConnection.html





原创粉丝点击