zoukankan      html  css  js  c++  java
  • ASIHTTPRequest详解 【经典】(二)

    转:http://blog.sina.com.cn/s/blog_801997310101bi9r.html

    ASINetworkQueues, 它的delegate提供更为丰富的功能

    提供的更多的回调方法如下:
    a,requestDidStartSelector,请求发起时会调此方法,你可以在此方法中跟据业务选择性的设置request对象的deleaget。
    b,requestDidReceiveResponseHeadersSelector,当接受完响应的Header后设计此方法,这个对下载大数据的时候相当有用,你可以在方法里做更多业务上的处理。
    c,requestDidFinishSelector,请求并响应成功完成时调用此方法
    d,requestDidFailSelector,请求失败
    e,queueDidFinishSelector,整个队列里的所有请求都结束时调用此方法。

    它是NSOperationQueues的扩展,小而强大。但也与它的父类略有区别。如,仅添加到队列中其实并不能执行请求,只有调用[ queue g o]才会执行;一个正在运行中的队列,并不需要重复调用[ queue go ]。

    默认情况下,队列中的一个请求如果失败,它会取消所有未完成的请求。可以设置[ queue setShouldCancelAllRequestsOnFailure:NO ]来修 正。

    取消异步请求

    首先,同步请求是不能取消的。
    其次,不管是队列请求,还是简单的异步请求,全部调用[ request cancel ]来取消请求。

    取消的请求默认都会按请求失败处理,并调用请求失败delegate。
    如果不想调用delegate方法,则设置:[ request clearDelegatesAndCancel];

    队列请求中需要注意的是,如果你取消了一个请求,队列会自动取消其它所有请求。
    如果只想取消一个请求,可以设置队列:[ queue setShouldCancelAllRequestsOnFailure:NO ];
    如果想明确取消所有请求:[ queue cancelAllOperations ];

    安全的内存回收建议

    request并没有retain你的delegate,所以在没有请求完的时候释放了此delegate,需要在dealloc方法里先取消所有请求,再释放请求实例,如:

    - (void)dealloc
    {
    [requestclearDelegatesAndCancel];
    [requestrelease];
    ...
    [superdealloc];
    }
    向服务器端上传数据

    ASIFormDataRequest ,模拟 Form表单提交,其提交格式与 Header会自动识别。
    没有文件:application/x-www-form-urlencoded
    有文件:multipart/form-data

    ASIFormDataRequest *request = [ASIFormDataRequestrequestWithURL:url];
    [request setPostValue:@"Ben"forKey:@"first_name"];
    [request setPostValue:@"Copsey"forKey:@"last_name"];
    [request setFile:@"/Users/ben/Desktop/ben.jpg"forKey:@"photo"];
    [request addData:imageData withFileName:@"george.jpg"andContentType:@"image/jpeg"forKey:@"photos"];

    如果要发送自定义数据:

    ASIHTTPRequest *request = [ASIHTTPRequestrequestWithURL:url];
    [request appendPostData:[@"Thisis my data" dataUsingEncoding:NSUTF8StringEncoding]];
    // Default becomes POST when you use appendPostData: /appendPostDataFromFile: / setPostBody:
    [request setRequestMethod:@"PUT"];
    下载文件

    通过设置request的setDownloadDestinationPath,可以设置下载文件用的下载目标目录。
    首先,下载过程文件会保存在temporaryFileDownloadPath目录下。如果下载完成会做以下事情:
    1,如果数据是压缩的,进行解压,并把文件放在downloadDestinationPath目录中,临时文件被删除
    2,如果下载失败,临时文件被直接移到downloadDestinationPath目录,并替换同名文件。

    如果你想获取下载中的所有数据,可以实现delegate中的request:didReceiveData:方法。但如果你实现了这个方法,request在下载完后,request并不把文件放在downloadDestinationPath中,需要手工处理。

    获取响应信息

    信息:status , header, responseEncoding

    [request responseStatusCode];
    [[request responseHeaders] objectForKey:@"X-Powered-By"];
    [requestresponseEncoding];
    获取请求进度

    有两个回调方法可以获取请求进度,
    1,downloadProgressDelegate,可以获取下载进度
    2,uploadProgressDelegate,可以获取上传进度

    cookie的支持

    如果Cookie存在的话,会把这些信息放在NSHTTPCookieStorage容器中共享,并供下次使用。
    你可以用[ ASIHTTPRequest setSessionCookies:nil ] ; 清空所有Cookies。
    当然,你也可以取消默认的Cookie策略,而使自定义的Cookie:

    //Create a cookie
    NSDictionary *properties = [[[NSMutableDictionary alloc] init]autorelease];
    [properties setValue:[@"TestValue" encodedCookieValue] forKey:NSHTTPCookieValue];
    [properties setValue:@"ASIHTTPRequestTestCookie"forKey:NSHTTPCookieName];
    [properties setValue:@".allseeing-i.com"forKey:NSHTTPCookieDomain];
    [properties setValue:[NSDatedateWithTimeIntervalSinceNow:60*60]forKey:NSHTTPCookieExpires];
    [properties setValue:@"/asi-http-request/tests"forKey:NSHTTPCookiePath];
    NSHTTPCookie *cookie = [[[NSHTTPCookie alloc]initWithProperties:properties] autorelease];
    //This url will return the value of the 'ASIHTTPRequestTestCookie'cookie
    request = [ASIHTTPRequest requestWithURL:url];
    [request setUseCookiePersistence:NO];
    [request setRequestCookies:[NSMutableArrayarrayWithObject:cookie]];
    [request startSynchronous];
    //Should be: I have 'Test Value' as the value of'ASIHTTPRequestTestCookie'
    NSLog(@"%@",[requestresponseString]);
  • 相关阅读:
    【poj3764】 The xor-longest Path
    【poj3261】 Milk Patterns
    【poj3237】 Tree
    【bzoj2654】 tree
    【poj3122】 Pie
    【poj1011】 Sticks
    【poj1186】 方程的解数
    【poj2741】 Colored Cubes
    【poj3141】 Distant Galaxy
    【bzoj2456】 mode
  • 原文地址:https://www.cnblogs.com/jackljf/p/3588940.html
Copyright © 2011-2022 走看看