iOS开源库解析之ASIHTTPRequest

本文介绍了iOS中已停更的ASIHTTPRequest库,特别是ASIFormDataRequest,它用于处理HTTP的POST请求。文章详细阐述了ASIFormDataRequest与父类ASIHTTPRequest的关系,包括它们的方法和特性,如postData、fileData、postFormat等属性,并分析了构建POST数据的buildPostBody方法。此外,还探讨了子类如何回调父类的方法,以及main方法在网络请求中的作用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

坚持 成长 每日一篇

前言

ASIHTTPRequest框架作者已经停更,今日公司ASIHTTPRequest从项目移除需要重构使用了ASIHTTPRequest的代码,要想重构ASIHTTPRequest最好对ASIHTTPRequest有所了解。今日花了2天时间看了ASIHTTPRequest源码,以此博客作为一些理解纪录

内容

ASIHTTPRequest和ASIFormDataRequest的关系

FormDataRequest是继承了httpRequest,他主要是针HTTP协议四种常见的POST方式的multipart/form-data请求和application/x-www-form-urlencoded请求。四种常见的请求详情http://www.aikaiyuan.com/6324.html

对于ASIHTTPRequest大家并不陌生,类似于iOS系统自带的request,这里主要介绍ASIFormDataRequest原理

ASIFormDataRequest特有属性

  1. postData:用来保存非文件的数据
  2. fileData:用来保存文件的数据
  3. debugBodyString:只是用于纪录组件HttpPostBody过程错误日志信息,没有其他作用,设置Body时候并没有用他
  4. postFormat:这是一个枚举属性,决定post是以form-data格式还是url encode格式上传数据
  5. stringEncoding:对于DebugBodyString转data时候采用的是何种编码。

ASIFormDataRequest.m实现文件里面的主要方法解释

三种设置PostData情况

普通文本类型

//普通文本类型最终会在这里加入所有的数据
- (void)addPostValue:(id <NSObject>)value forKey:(NSString *)key
{
    if (!key) {
        return;
    }
    if (![self postData]) {
        [self setPostData:[NSMutableArray array]];
    }
    NSMutableDictionary *keyValuePair = [NSMutableDictionary dictionaryWithCapacity:2];
    [keyValuePair setValue:key forKey:@"key"];
    [keyValuePair setValue:[value description] forKey:@"value"];
    [[self postData] addObject:keyValuePair];
}
//此方法最终调用上面的addPostValue:forKey:来给postData添加字典,postData在构建PostBody时候会被用到
- (void)setPostValue:(id <NSObject>)value forKey:(NSString *)key
{
    // Remove any existing value
    NSUInteger i;
    for (i=0; i<[[self postData] count]; i++) {
        NSDictionary *val = [[self postData] objectAtIndex:i];
        if ([[val objectForKey:@"key"] isEqualToString:key]) {
            [[self postData] removeObjectAtIndex:i];
            i--;
        }
    }
    [self addPostValue:value forKey:key];
}

本地文件路径,最终添加是数据到fileData

//无论你是addFileValue还是setFileValue最终都会调用这个方法来组织文件数据
- (void)addFile:(NSString *)filePath withFileName:(NSString *)fileName andContentType:(NSString *)contentType forKey:(NSString *)key
{
    BOOL isDirectory = NO;
    BOOL fileExists = [[[[NSFileManager alloc] init] autorelease] fileExistsAtPath:filePath isDirectory:&isDirectory];
    if (!fileExists || isDirectory) {
        [self failWithError:[NSError errorWithDomain:NetworkRequestErrorDomain code:ASIInternalErrorWhileBuildingRequestType userInfo:[NSDictionary dictionaryWithObjectsAndKeys:[NSString stringWithFormat:@"No file exists at %@",filePath],NSLocalizedDescriptionKey,nil]]];
    }

    // If the caller didn't specify a custom file name, we'll use the file name of the file we were passed
    if (!fileName) {
        fileName = [filePath lastPathComponent];
    }

    // If we were given the path to a file, and the user didn't specify a mime type, we can detect it from the file extension
    if (!contentType) {
        contentType = [ASIHTTPRequest mimeTypeForFileAtPath:filePath];
    }
    [self addData:filePath withFileName:fileName andContentType:contentType forKey:key];
}

最后一种就是用于如果你手中已经有了文件数据,可以通过下列方法组织到fileData里面,包括上面介绍的file方法最终也是屌用此方法来

- (void)addData:(id)data withFileName:(NSString *)fileName andContentType:(NSString *)contentType forKey:(NSString *)key
{
    if (![self fileData]) {
        [self setFileData:[NSMutableArray array]]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值