1. 程式人生 > >自薦親自寫的一套http訪問類庫推薦給大家使用。

自薦親自寫的一套http訪問類庫推薦給大家使用。

failed os x 一個 job val rac frame multi font

本人在開發過程中,由於各種原因。

自己開發了一套HTTP訪問工具。該工具借監了眼下大家都使和的ASI類庫。個人感認為ASI裏面太多的控制及邏輯變量都寫在同一個類中。沒有非常好的規劃好。同一時候可能是由於多個人的改動,使得ASI不同的版本號出現了不同的兼容方式。

同一時候也不可避免的增輯了對該代碼的維護。

出於本人的開發經驗自己寫了一套這種類庫,基本上與ASI的功能上差不太多,可是對存在的未知BUG及使用過程中出現的問題,也希望各讀者在使用時將BUG反饋給我,以便修正。

同一時候我把這套原碼放在我的GITHUB上。供大家使用。希望大家多多支持我。

GITHUB地址:https://github.com/fengsh998/WebRequestComponent

最新改動已支持block回調。


該類庫使用簡單。代碼通俗易懂。使用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 =[FQWebRequestrequestWithURL:@"http://nanjing.baixing.com/oz/login/x"];

??? NSMutableDictionary *items = [NSMutableDictionarydictionaryWithObjectsAndKeys:@"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:[NSMutableDatadataWithData:data]];

??? test22.delegate = self;

??? [test22.requestHeader setRequestHeaderByDictionary:items];

??? [test22 setRequestMethod:requestUsePost];

???[test22 go];

?

?

GET 例子:

?

??? NSMutableDictionary *items = [NSMutableDictionarydictionaryWithObjectsAndKeys:@"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 =[FQWebRequestrequestWithURL:@"http://www.google.com.hk"];

???

??? [test1.requestHeader setRequestHeaderByDictionary:items];

??? [test1 setRequestMethod:requestUseGet];

??? //test1.autoSaveUseCookies=YES;

???[test1 go];

?

?

類點下載例子:

?

weq = [FQDownLoadRequestrequestWithURL:@"http://a.tgbus.com/download/33747/1"];

??? NSMutableDictionary *items = [NSMutableDictionarydictionaryWithObjectsAndKeys:

????????????????????????????????? @"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.requestHeadersetRequestHeaderByDictionary:items];

??? //[weq setRequestMethod:requestUseGet];

??? weq.delegate =self;

??? FQWebProxySettings *proxy = [[FQWebProxySettingsalloc]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 = [FQUploadRequestrequestWithURL:@"http://www.mftp.info/upload.php"];

??? NSMutableDictionary *items = [NSMutableDictionarydictionaryWithObjectsAndKeys:@"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 = [[FormDataPackagealloc]init];

??? MimePart *filepart = [[MimePartalloc]init];

??? [filepart addMimeHeader:@"Content-Disposition"withValue:@"form-data;name=\"MAX_FILE_SIZE\""];

??? filepart.bodystring = @"1000000";

???

??? MimePart *filepart1 = [[MimePartalloc]init];

??? [filepart1 addMimeHeader:@"Content-Disposition"withValue:@"form-data;name=\"uploadimg\"; filename=\"銀行大劫案BankJob.apk\""];

??? [filepart1 addMimeHeader:@"Content-Type"withValue:@"application/octet-stream"];

???

??? NSMutableData *md = [NSMutableDatadataWithContentsOfFile:@"/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 = [FQUploadRequestrequestWithURL:@"http://www.dumpt.com/img/upload.php"];

??? NSMutableDictionary *items = [NSMutableDictionarydictionaryWithObjectsAndKeys:@"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 = [[FormDataPackagealloc]init];

??? dp.postBodyType = postformMultipartData;

???

??? MimePart *filepart = [[MimePartalloc]init];

??? [filepart addMimeHeader:@"Content-Disposition"withValue:@"form-data;name=\"type\""];

??? filepart.bodystring = @"direct";

???

??? MimePart *filepart1 = [[MimePartalloc]init];

??? [filepart1 addMimeHeader:@"Content-Disposition"withValue:@"form-data;name=\"userfile[]\"; filename=\"2.png\""];

??? [filepart1 addMimeHeader:@"Content-Type"withValue:@"image/png"];

???

??? NSMutableData *md = [NSMutableDatadataWithContentsOfFile:@"/Users/apple/Desktop/2.png"];

??? filepart1.bodyFileData = md;

???

??? MimePart *filepart2 = [[MimePartalloc]init];

??? [filepart2 addMimeHeader:@"Content-Disposition"withValue:@"form-data;name=\"userfile[]\"; filename=\"\""];

??? [filepart2 addMimeHeader:@"Content-Type"withValue:@"application/octet-stream"];

???

??? MimePart *filepart3 = [[MimePartalloc]init];

??? [filepart3 addMimeHeader:@"Content-Disposition"withValue:@"form-data;name=\"userfile[]\"; filename=\"\""];

??? [filepart3 addMimeHeader:@"Content-Type"withValue:@"application/octet-stream"];

???

???

??? MimePart *filepart4 = [[MimePartalloc]init];

??? [filepart4 addMimeHeader:@"Content-Disposition"withValue:@"form-data;name=\"userfile[]\"; filename=\"\""];

??? [filepart4 addMimeHeader:@"Content-Type"withValue:@"application/octet-stream"];

???

???

??? MimePart *filepart5 = [[MimePartalloc]init];

??? [filepart5 addMimeHeader:@"Content-Disposition"withValue:@"form-data;name=\"userfile[]\"; filename=\"\""];

??? [filepart5 addMimeHeader:@"Content-Type"withValue:@"application/octet-stream"];

???

??? MimePart *filepart6 = [[MimePartalloc]init];

??? [filepart6 addMimeHeader:@"Content-Disposition"withValue:@"form-data;name=\"x\""];

??? filepart6.bodystring = @"64";

???

??? MimePart *filepart7 = [[MimePartalloc]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訪問類庫推薦給大家使用。