• NSURLRequest和NSURLConnection


    NSURLRequest objects represent a URL load request in a manner independent of protocol and URL scheme.

    NSURLRequest encapsulates two basic data elements of a load request: the URL to load, and the policy to use when consulting the URL content cache made available by the implementation.

    NSURLRequest is designed to be extended to support additional protocols by adding categories that access protocol specific values from a property object using NSURLProtocol’s propertyForKey:inRequest: and setProperty:forKey:inRequest: methods.

    The mutable subclass of NSURLRequest is NSMutableURLRequest.

      NSURLRequest对象代表一个URL加载请求的方式独立于协议和URL方案。

      NSURLRequest封装两个基本数据元素的加载要求:URL加载和政策咨询时使用的URL缓存提供的内容实现。

      NSURLRequest被设计成可扩展以支持额外的协议通过添加类别,从一个属性对象访问协议特定的值使用NSURLProtocol propertyForKey:inRequest:和setProperty:forKey:inRequest:方法。

      NSURLRequest的可变子类NSMutableURLRequest。


    NSURLConnection provides the most flexible method of retrieving the contents of a URL. This class provides a simple interface for creating and canceling a connection, and supports a collection of delegate methods that provide feedback and control of many aspects of the connection. These classes fall into five categories: URL loading, cache management, authentication and credentials, cookie storage, and protocol support.

    NSURLConnection提供最灵活的方法检索URL的内容。这个类提供了一个简单的接口用于创建和取消连接,并支持委派方法的集合,提供反馈和控制连接的许多方面。这些类可分为五类:URL加载、缓存管理、身份验证和凭证,cookie存储和协议的支持。


    NSURLRequest默认的cache policy是NSURLRequestUseProtocolCachePolicy, 是最能保持一致性的协议。

    NSURLRequestReloadIgnoringCacheData 忽略缓存直接从原始地址下载

    NSURLRequestReturnCacheDataElseLoad 只有在cache中不存在data时才从原始地址下载

    NSURLRequestReturnCacheDataDontLoad 允许app确定是否要返回cache数据,如果使用这种协议当本地不存在response的时候,创建NSURLConnection or NSURLDownload实例时将会马上返回nil;这类似于离线模式,没有建立网络连接;


    你只需要实现以下delegate方法来处理数据响应


    - (void)connection:(NSURLConnection*)connection didReceiveResponse:(NSHTTPURLResponse*)response

    - (void)connection:(NSURLConnection*)connection didReceiveData:(NSData*)data

    - (void)connection:(NSURLConnection*)connection didFailWithError:(NSError*)error

    - (void)connectionDidFinishLoading:(NSURLConnection *)connection

    NSURLConnect还提供了一个方便的类方法(class method) : sendSynchronousRequest:returningResponse:error:可用来同步加载一个URL请求

    + (NSData *)sendSynchronousRequest:    (NSURLRequest *)request      returningResponse:   (NSURLResponse **)response    error:  (NSError **)error

    • request 要装载的URL请求. 这个request 对象 作为初始化进程的一部分,被深度复制(deep-copied). 在这个方法返回之后, 再修改request, 将不会影响用在装载的过程中的request

    • reponse 输出参数, 由服务器返回的URL响应

    • error   输出参数, 如果在处理请求的过程中发生错误,就会使用.  无错误,就为NULL

  • 相关阅读:
    C++各大有名库的介绍——网络通信
    C++名人的网站
    C++各大有名库的介绍——序列化
    C++各大有名库的介绍——字符串
    C++各大有名库的介绍——游戏开发
    C++各大有名库的介绍——其他库
    C++各大有名库的介绍——综合
    java hashcode写法
    vs 在线引用
    eclipse去掉拼写检查
  • 原文地址:https://www.cnblogs.com/lkjson/p/4337761.html
Copyright © 2020-2023  润新知