zoukankan      html  css  js  c++  java
  • ASIHttpRequest 摘要

    向server端上传数据
    ASIFormDataRequest ,模拟 Form表单提交,其提交格式与 Header会自己主动识别。
    没有文件:application/x-www-form-urlencoded
    有文件:multipart/form-data
    ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL: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 = [ASIHTTPRequest requestWithURL:url];
    [request appendPostData:[@"This is 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"];
    [request responseEncoding];
    获取请求进度
    有两个回调方法能够获取请求进度。
    1,downloadProgressDelegate,能够获取下载进度
    2,uploadProgressDelegate,能够获取上传进度
    cookie的支持
    假设Cookie存在的话,会把这些信息放在NSHTTPCookieStorage容器中共享,并供下次使用。
    你能够用[ ASIHTTPRequest setSessionCookies:nil ] ; 清空全部Cookies。
    当然。你也能够取消默认的Cookie策略,而使自己定义的Cookie:
    //Create a cookie
    NSDictionary *properties = [[[NSMutableDictionary alloc] init] autorelease];
    [properties setValue:[@"Test Value" encodedCookieValue] forKey:NSHTTPCookieValue];
    [properties setValue:@"ASIHTTPRequestTestCookie" forKey:NSHTTPCookieName];
    [properties setValue:@".allseeing-i.com" forKey:NSHTTPCookieDomain];
    [properties setValue:[NSDate dateWithTimeIntervalSinceNow: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:[NSMutableArray arrayWithObject:cookie]];
    [request startSynchronous];

    //Should be: I have 'Test Value' as the value of 'ASIHTTPRequestTestCookie'
    NSLog(@"%@",[request responseString]);
    大文件断点续传
    0.94以后支持大文件的断点下载。仅仅须要设置:
    [ request setAllowResumeForFileDownloads:YES ];
    [ request setDownloadDestinationPath:downloadPath ];
    就能够了。
    ASIHTTPRequest会自己主动保存訪问过的URL信息。并备之后用。在下面几个场景很实用:
    1。当没有网络连接的时候。
    2,已下载的数据再次请求时,仅当它与本地版本号不样时才进行下载。
    ASIDownloadCache 设置下载缓存
    它对Get请求的响应数据进行缓存(被缓存的数据必需是成功的200请求):
    [ASIHTTPRequest setDefaultCache:[ASIDownloadCache sharedCache]];
    当设置缓存策略后。全部的请求都被自己主动的缓存起来。


    另外。假设只希望某次请求使用缓存操作,也能够这样使用:

    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
    [request setDownloadCache:[ASIDownloadCache sharedCache]];
    多种的缓存并存
    只须要创建不同的ASIDownloadCache,并设置缓存所使用的路径,并设置到须要使用的request实例中:
    ASIDownloadCache *cache = [[[ASIDownloadCache alloc] init] autorelease];
    [cache setStoragePath:@"/Users/ben/Documents/Cached-Downloads"];
    [self setMyCache:cache];
    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
    [request setDownloadCache:[self myCache]];
    缓存策略
    缓存策略是我们控制缓存行为的主要方式,如:什么时候进行缓存,缓存数据的利用方式。
    下面是策略可选列表(可组合使用):
    ASIUseDefaultCachePolicy
    这是一个默认的缓存策略“ASIAskServerIfModifiedWhenStaleCachePolicy”,这个非常明确,见名知意(它不能与其他策略组合使用)
    ASIDoNotReadFromCacheCachePolicy
    所读数据不使用缓存
    ASIDoNotWriteToCacheCachePolicy
    不正确缓存数据进行写操作
    ASIAskServerIfModifiedWhenStaleCachePolicy
    默认缓存行为,request会先推断是否存在缓存数据。a, 假设没有再进行网络请求。 b。假设存在缓存数据,而且数据没有过期。则使用缓存。c,假设存在缓存数据。但已经过期。request会先进行网络请求,推断server版本号与本地版本号是否一样,假设一样,则使用缓存。假设server有新版本号,会进行网络请求,并更新本地缓存
    ASIAskServerIfModifiedCachePolicy
    与默认缓存大致一样,差别仅是每次请求都会 去server推断是否有更新
    ASIOnlyLoadIfNotCachedCachePolicy
    假设有缓存在本地,无论其过期与否,总会拿来使用
    ASIDontLoadCachePolicy
    仅当有缓存的时候才会被正确运行,假设没有缓存,request将被取消(没有错误信息)
    ASIFallbackToCacheIfLoadFailsCachePolicy
    这个选项常常被用来与其他选项组合使用。请求失败时。假设有缓存当网络则返回本地缓存信息(这个在处理异常时很实用)
    假设设置了“defaultCachePolicy”则全部的请求都会使用此缓存。

    缓存存储方式
    你能够设置缓存的数据须要保存多长时间。ASIHTTPRequest提供了两种策略:
    a。ASICacheForSessionDurationCacheStoragePolicy。默认策略,基于session的缓存数据存储。

    当下次执行或[ASIHTTPRequest clearSession]时,缓存将失效。
    b,ASICachePermanentlyCacheStoragePolicy,把缓存数据永久保存在本地。
    如:

    ASIHTTPRequest *request = [ ASIHTTPRequest requestWithURL:url ];
    [ request setCacheStoragePolicy:ASICachePermanentlyCacheStoragePolicy ];
    另外,也能够使用clearCachedResponsesForStoragePolicy来清空指定策略下的缓存数据。
    缓存其他特性
    设置是否按server在Header里指定的是否可被缓存或过期策略进行缓存:
    [[ ASIDownloadCache sharedCache ] setShouldRespectCacheControlHeaders:NO ];
    设置request缓存的有效时间:
    [ request setSecondsToCache:60*60*24*30 ]; // 缓存30天
    能够推断数据是否从缓存读取:
    [ request didUseCachedResponse ];
    设置缓存所使用的路径:
    [ request setDownloadDestinationPath:[[ ASIDownloadCache sharedCache ] pathToStoreCachedResponseDataForRequest:request ]];
    实现自己定义的缓存
    仅仅要简单的实现ASICacheDelegate接口就能够被用来使用。

    使用代理请求
    默认的情况下。ASIHTTPRequest会使用被设置的默认代理。

    但你也能够手动改动http代理:

    // Configure a proxy server manually
    NSURL *url = [ NSURL URLWithString:@"http://allseeing-i.com/ignore" ];
    ASIHTTPRequest *request = [ ASIHTTPRequest requestWithURL:url ];
    [ request setProxyHost:@"192.168.0.1" ];
    [ request setProxyPort:3128 ];

    // Alternatively, you can use a manually-specified Proxy Auto Config file (PAC)
    // (It's probably best if you use a local file)
    [request setPACurl:[NSURL URLWithString:@"file:///Users/ben/Desktop/test.pac"]];
    ASIHTTPRequest, 请求的其他特性
    iOS4中,当应用后台执行时仍然请求数据:
    [ request setShouldContinueWhenAppEntersBackground:YES ];
    是否有网络请求:
    [ ASIHTTPRequest isNetworkInUse ]
    是否显示网络请求信息在status bar上:
    [ ASIHTTPRequest setShouldUpdateNetworkActivityIndicator:NO ];
    设置请求超时时,设置重试的次数:
    [ request setNumberOfTimesToRetryOnTimeout:2 ];
    KeepAlive的支持:
         
    // Set the amount of time to hang on to a persistent connection before it should expire to 2 minutes
    [ request setPersistentConnectionTimeoutSeconds:120 ];

    // Disable persistent connections entirely
    [ request setShouldAttemptPersistentConnection:NO ];
  • 相关阅读:
    经典脚本集合
    Crystal Report 注册号
    Linux top命令简介
    sysctl.conf优化方案(完整)
    vi入门学习(转载)
    linux 如何查看目录的剩余空间大小?
    python3安装pip3
    jsonCpp的readme文档
    第15天android:使用sqlite
    《mysql必知必会》笔记
  • 原文地址:https://www.cnblogs.com/bhlsheji/p/5135262.html
Copyright © 2011-2022 走看看