本人在开发过程中,因为各种原因。自己开发了一套HTTP访问工具,该工具借监了目前大家都使和的ASI类库。个人感觉得ASI里面太多的控制及逻辑变量都写在同一个类中,没有很好的规划好。同时可能是由于多个人的修改,使得ASI不同的版本出现了不同的兼容方式。同时也不可避免的增辑了对该代码的维护。
出于本人的开发经验自己写了一套这样的类库,基本上与ASI的功能上差不太多,但是对存在的未知BUG及使用过程中出现的问题,也希望各读者在使用时将BUG反馈给我,以便修正。同时我把这套原码放在我的GITHUB上。供大家使用。希望大家多多支持我。
GITHUB地址:https://github.com/fengsh998/WebRequestComponent
该类库使用简单,代码通俗易懂,使用OOP进行划分,使得结构更加清晰,更易维护。
类库的类结构图:
类关系图:
类库的运行原理图:
类库说明:
1. 使用GCD线程管理,使用异步请求方式(暂不支持同步请求)。
2. 支持SSL,proxy(http,socket5),cookie。
3. 支持http断点续传(下载),支持上传、下载进度回调。
4. 支持队列请求,并发请求,组请求。
5. 支持常见http请求,(POST,GET,DELETE,PUT)
6. 支持重定向。
7. 支持GZIP请求。
类库为非ARC模式。
类库支持环境:
IOS:
Xcode 4.6以上。
Mac 10.8.5 xcode5.0以上。只支持64位编译,32位机的没有做适配。所以低版本的XC编译会有问题。
类库依赖:
依赖:
ios :UIKit.framework,mobileCoreServices.framework
mac :systemConfiguration.framework,cocoa.framework,appkit.framework
sdk依赖(ios,mac共同依赖):CFNewwork.framework,libz.dylib,Foundation.framework
类库的代理说明:
@protocolFQWebRequestDelegate <NSObject>
@optional
- (void)requestStarted:(FQWebRequest *)request;
- (void)requestFinished:(FQWebRequest *)request;
- (void)requestFailed:(FQWebRequest *)request;
/*
当前请求需要证书认证时
字点中使用的Key为
FQWebAuthenticationUsername
FQWebAuthenticationPassword
*/
- (NSDictionary*)authenticationNeededForRequest:(FQWebRequest *)request;
- (NSDictionary*)proxyAuthenticationNeededForRequest:(FQWebRequest *)request;
/*
该网站的安全证书不受信任!此时提示仍然继续还是返回安全连接
如果返回True,则使用不安全连接继续访问,FALSE则此次访问失败
*/
- (BOOL)isContinueWhenUnsafeConnectInCureentRequest:(FQWebRequest *)request;
/*
接收到的进度
*/
- (void)requestReceviceProgress:(FQWebRequest *)request
withTotalSize:(FQULLInteger) total withRecvicedSize:(FQULLInteger)size;
/*
发送进度
*/
- (void)requestSendProgress:(FQWebRequest *)request
withTotalSize:(FQULLInteger) total withSendSize:(FQULLInteger)size;
@end
@protocolFQWebRequestProgressDelegate <FQWebRequestDelegate>
@optional
/*
当有数据下发时会触发
*/
- (void)downloadProgress:(FQWebRequest *)request
withTotalSize:(FQULLInteger) total
withRecvicedSize:(FQULLInteger)size;
/*
当有post数据时会触发
*/
- (void)uploadProgress:(FQWebRequest *)request
withTotalSize:(FQULLInteger) total
withUploadsize:(FQULLInteger)size;
@end
@protocol FQWebRequestInGroupDelegate<NSObject>
@optional
- (void)allRequestFinish:(FQWebGroupRequest *)groupRequest;
@end
目前只支持使用delegate进行回调,暂不支持block和SEL的形式进行回调。
POST 样例:
FQWebRequest *test22 =[FQWebRequest
requestWithURL:@"http://nanjing.baixing.com/oz/login/x"];
NSMutableDictionary *items = [NSMutableDictionary
dictionaryWithObjectsAndKeys:@"keep-alive",@"Connection",
@"*/*",@"Accept",
@"gzip,deflate",@"Accept-Encoding",
@"zh-CN,zh;q=0.8,en;q=0.6",@"Accept-Language",
@"application/x-www-form-urlencoded",@"Content-Type",
nil];
NSString *pd =
@"identity=fengsh998&password=13870021792&token=a86d0f446368b94e97f83b309d8cb303";
NSData *data = [pd
dataUsingEncoding:NSUTF8StringEncoding];
[test22 setPostData:[NSMutableData
dataWithData:data]];
test22.delegate =
self;
[test22.requestHeader
setRequestHeaderByDictionary:items];
[test22 setRequestMethod:requestUsePost];
[test22 go];
GET 样例:
NSMutableDictionary *items = [NSMutableDictionary
dictionaryWithObjectsAndKeys:@"keep-alive",@"Connection",
@"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",@"Accept",
@"gzip,deflate",@"Accept-Encoding",
//@"gzip",@"Accept-Encoding",
@"zh-CN,zh;q=0.8,en;q=0.6",@"Accept-Language",
nil];
FQWebRequest *test1 =[FQWebRequest
requestWithURL:@"http://www.google.com.hk"];
[test1.requestHeader
setRequestHeaderByDictionary:items];
[test1 setRequestMethod:requestUseGet];
//test1.autoSaveUseCookies=YES;
[test1 go];
类点下载样例:
weq = [FQDownLoadRequest
requestWithURL:@"http://a.tgbus.com/download/33747/1"];
NSMutableDictionary *items = [NSMutableDictionary
dictionaryWithObjectsAndKeys:
@"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",@"Accept",
@"gzip,deflate,sdch",@"Accept-Encoding",
@"zh-CN,zh;q=0.8,en;q=0.6",@"Accept-Language",
//@"keep-alive",@"Connection",
//@"User-Agent",@"Mozilla/5.0 (Macintosh;Intel Mac OS X 10_8_5)",
//@"",@"",
nil];
[weq
setDownloadStorePath:@"/Users/apple/Desktop/FQDownload"];
[weq.requestHeader
setRequestHeaderByDictionary:items];
//[weq setRequestMethod:requestUseGet];
weq.delegate =
self;
FQWebProxySettings *proxy = [[FQWebProxySettings
alloc]init];
//weq.proxySettings = proxy;
[proxy release];
//weq.proxySettings.proxyHost [email protected]"211.138.121.38";
//weq.proxySettings.proxyPort = 81;
//weq.proxySettings.proxyType = wpProxyHttp;
//weq.useCustomSaveFileName = @"aa.apk";
[weq
setReDownloadFile:@"银行大劫案BankJob.apk.FQDownload" useResume:YES];
[weq go];
POST FORM表单上传样例:
FQUploadRequest *rq = [FQUploadRequest
requestWithURL:@"http://www.mftp.info/upload.php"];
NSMutableDictionary *items = [NSMutableDictionary
dictionaryWithObjectsAndKeys:@"keep-alive",@"Connection",
@"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",@"Accept",
@"gzip,deflate",@"Accept-Encoding",
@"zh-CN,zh;q=0.8,en;q=0.6",@"Accept-Language",
@"application/x-www-form-urlencoded",@"Content-Type",
nil];
[rq.requestHeader
setRequestHeaderByDictionary:items];
FormDataPackage *dp = [[FormDataPackage
alloc]init];
MimePart *filepart = [[MimePart
alloc]init];
[filepart addMimeHeader:@"Content-Disposition"
withValue:@"form-data;name=\"MAX_FILE_SIZE\""];
filepart.bodystring =
@"1000000";
MimePart *filepart1 = [[MimePart
alloc]init];
[filepart1 addMimeHeader:@"Content-Disposition"
withValue:@"form-data;name=\"uploadimg\"; filename=\"银行大劫案BankJob.apk\""];
[filepart1 addMimeHeader:@"Content-Type"
withValue:@"application/octet-stream"];
NSMutableData *md = [NSMutableData
dataWithContentsOfFile:@"/Users/apple/Desktop/FQDownload/银行大劫案BankJob.apk.FQDownload"];
filepart1.bodyFileData = md;
[dp addMultiPart:filepart];
[dp addMultiPart:filepart1];
dp.postBodyType =
postformMultipartData;
//NSData *dt = [dpbuildMultipartFormDataPostBody];
//[rq setRequestMethod:requestUsePost];
//[rq.requestHeadersetRequestHeader:@"Content-Type" value:dp.contentTypeValue];
rq.delegate =
self;
//[rq setPostData:[NSMutableDatadataWithData:dt]];
//[rqsetPostBodyFromFile:@"/Users/apple/Desktop/FQDownload/银行大劫案BankJob.apk.FQDownload"];
[rq setuploadFormData:dp];
[rq go];
对于multi表单类型的form:
FQUploadRequest *rq = [FQUploadRequest
requestWithURL:@"http://www.dumpt.com/img/upload.php"];
NSMutableDictionary *items = [NSMutableDictionary
dictionaryWithObjectsAndKeys:@"keep-aflive",@"Connection",
@"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",@"Accept",
@"gzip,deflate",@"Accept-Encoding",
@"zh-CN,zh;q=0.8,en;q=0.6",@"Accept-Language",
@"application/x-www-form-urlencoded",@"Content-Type",
nil];
[rq.requestHeader
setRequestHeaderByDictionary:items];
FormDataPackage *dp = [[FormDataPackage
alloc]init];
dp.postBodyType =
postformMultipartData;
MimePart *filepart = [[MimePart
alloc]init];
[filepart addMimeHeader:@"Content-Disposition"
withValue:@"form-data;name=\"type\""];
filepart.bodystring =
@"direct";
MimePart *filepart1 = [[MimePart
alloc]init];
[filepart1 addMimeHeader:@"Content-Disposition"
withValue:@"form-data;name=\"userfile[]\"; filename=\"2.png\""];
[filepart1 addMimeHeader:@"Content-Type"
withValue:@"image/png"];
NSMutableData *md = [NSMutableData
dataWithContentsOfFile:@"/Users/apple/Desktop/2.png"];
filepart1.bodyFileData = md;
MimePart *filepart2 = [[MimePart
alloc]init];
[filepart2 addMimeHeader:@"Content-Disposition"
withValue:@"form-data;name=\"userfile[]\"; filename=\"\""];
[filepart2 addMimeHeader:@"Content-Type"
withValue:@"application/octet-stream"];
MimePart *filepart3 = [[MimePart
alloc]init];
[filepart3 addMimeHeader:@"Content-Disposition"
withValue:@"form-data;name=\"userfile[]\"; filename=\"\""];
[filepart3 addMimeHeader:@"Content-Type"
withValue:@"application/octet-stream"];
MimePart *filepart4 = [[MimePart
alloc]init];
[filepart4 addMimeHeader:@"Content-Disposition"
withValue:@"form-data;name=\"userfile[]\"; filename=\"\""];
[filepart4 addMimeHeader:@"Content-Type"
withValue:@"application/octet-stream"];
MimePart *filepart5 = [[MimePart
alloc]init];
[filepart5 addMimeHeader:@"Content-Disposition"
withValue:@"form-data;name=\"userfile[]\"; filename=\"\""];
[filepart5 addMimeHeader:@"Content-Type"
withValue:@"application/octet-stream"];
MimePart *filepart6 = [[MimePart
alloc]init];
[filepart6 addMimeHeader:@"Content-Disposition"
withValue:@"form-data;name=\"x\""];
filepart6.bodystring =
@"64";
MimePart *filepart7 = [[MimePart
alloc]init];
[filepart7 addMimeHeader:@"Content-Disposition"
withValue:@"form-data;name=\"y\""];
filepart7.bodystring =
@"13";
[dp addMultiPart:filepart];
[dp addMultiPart:filepart1];
[dp addMultiPart:filepart2];
[dp addMultiPart:filepart3];
[dp addMultiPart:filepart4];
[dp addMultiPart:filepart5];
[dp addMultiPart:filepart6];
[dp addMultiPart:filepart7];
[rq setuploadFormData:dp];
rq.delegate =
self;
[rqgo];
本人亲自写的一套http访问类库推荐给大家使用。