1.NSURLProtocol 详解
2.ios afnetworkingåç
3.å¦ä½å¤çnsurlconnectionè·å¾çdata
4.NSMutableURLRequestå¦ä½è®¾ç½®headerï¼
NSURLProtocol 详解
åè¡·æ¯ä¸ºäºè®© UIWebView å¯ä»¥æ¦æª Ajax 请æ±ãç 究äºä¸çªæ¾å°äº NSURLProtocolãNSURLProtocol æ¯å±äº Foundation æ¡æ¶éç URL Loading System çä¸é¨åï¼å®æ¯ä¸ä¸ªæ½è±¡ç±»,源码远程打印源码ä¸è½å»å®ä¾åå®,åªè½åç±»åNSURLProtocolï¼ç¶å使ç¨çæ¶å注ååç±»ãä¸ä¸ªç¸å¯¹æ¦æ¶©é¾è§£çç±»ã
é£ä¹å¦æå¼åè èªå®ä¹çä¸ä¸ª NSURLProtocol 并ä¸æ³¨åå° app ä¸ï¼é£ä¹å¨è¿ä¸ªèªå®ä¹ç NSURLProtocol ä¸æ们å¯ä»¥æ¦æªææç请æ±ï¼è¿è¡ä¿®æ¹ï¼æè ä¿®æ¹ responseã
NSHipster ä¸è¯´ï¼ãæè è¿ä¹è¯´å§ï¼ NSURLProtocol å°±æ¯ä¸ä¸ªè¹æå 许çä¸é´äººæ»å»ãã
ç±äº NSURLProtocol æ¯ä¸ä¸ªæ½è±¡ç±»ï¼æ以使ç¨çæ¶åå¿ é¡»å®ä¹ä¸ä¸ªå®çåç±»ï¼
NSURLConnection å起请æ±çæ¶åï¼ä¼è®©ææ已注åç URLProtocol æ¥â审æ¹âè¿ä¸ªè¯·æ±
注æ: å¦ææ¯åºäº NSURLSession è¿è¡ç请æ±ï¼æ³¨åçæ¶åéè¦æ³¨åå° NSURLSessionConfiguration ä¸ï¼
è®°å¾ç¨å®ä¹å注éï¼
注åæåä¹åï¼å°±éè¦æ们çåç±»å»å®ç°æ½è±¡æ¹æ³ï¼
ç¨æ¥ å®¡æ¹ çæ¹æ³ã
åé¢è¯´å°ãNSURLConnection å起请æ±çæ¶åï¼ä¼è®©ææ已注åç URLProtocol æ¥â审æ¹âè¿ä¸ªè¯·æ±ããè¿éè¿åNO代表æ¾è¿è¿ä¸ªè¯·æ±ï¼ä¸ä½å¤çãè¿åYESï¼ä»£è¡¨éè¦å¤çï¼åä¼è¿å ¥åç»çæµç¨ã
注æï¼è¿ééè¦æ¾è¿å·²ç»å¤çè¿ç请æ±ï¼
è¿ä¸ªæ¹æ³ç¨æ¥ç»ä¸å¤çè¯·æ± request 对象çï¼å¯ä»¥ä¿®æ¹å¤´ä¿¡æ¯ï¼æè éå®åã没æç¹æ®éè¦ï¼åç´æ¥return request;
å¦æè¦å¨è¿éåéå®å以å头信æ¯çæ¶å注ææ£æ¥æ¯å¦å·²ç»æ·»å ï¼å 为è¿ä¸ªæ¹æ³å¯è½è¢«è°ç¨å¤æ¬¡ï¼ä¹å¯ä»¥å¨åé¢çæ¹æ³ä¸åã
å¤æç½ç»è¯·æ±æ¯å¦ä¸è´ï¼ä¸è´çè¯ä½¿ç¨ç¼åæ°æ®ã没éè¦å°±è°ç¨ super çæ¹æ³ã
åç±»ä¸æéè¦çæ¹æ³å°±æ¯ -startLoading å -stopLoadingï¼å®ç°è¯·æ±ååæ¶æµç¨ãä¸åçèªå®ä¹åç±»å¨è°ç¨è¿ä¸¤ä¸ªæ¹æ³æ¯ä¼ä¼ å ¥ä¸åçå 容ï¼ä½å ±åç¹é½æ¯è¦å´ç» protocol 客æ·ç«¯è¿è¡æä½ã
å¯ä»¥å¨è¿éä¿®æ¹è¯·æ±ä¿¡æ¯ï¼éå®åï¼DNS解æï¼è¿åèªå®ä¹çæµè¯æ°æ®ã
éç¹ï¼éè¦æ è®°å·²ç»å¤çè¿ç requestï¼
URLProtocolHandledKey æ¯:
举ä¾ï¼ç´æ¥å¨ startLoading ä¸è¿åæµè¯æ°æ®ï¼
éè¦æ³¨æçæ¯ç¶ç±»ä¸æä¸ä¸ª client å±æ§ã
å®ç°çåè®® <NSURLProtocolClient> å¦ä¸ï¼
对äºéè¦å¤çç connectionï¼å¯ä»¥å¨ä¸ç NSURLConnectionDataDelegate ä¸è¿è¡æä½ï¼
å¦ææ们顺åºæ³¨å A B C ä¸ä¸ª Protocolï¼é£ä¹ä¸ä¸ª connection å¨åéçæ¶åï¼å¤çç顺åºæ¯ C B Aï¼èä¸æå¤åªæä¸ä¸ª Protocol ä¼è§¦åå¤çã
æ¦æª UIWebview ç请æ±ï¼ä¼æ被æçé£é©ã
注ææ è®°å¤çè¿çï¼å ·ä½åæ³å¨æ¬ææå ³é®è¯ URLProtocolHandledKey ã
ios afnetworkingåç
AFNetworking主è¦æ¯å¯¹NSURLSessionåNSURLConnection(iOS9.0åºå¼)çå°è£ ,å ¶ä¸ä¸»è¦æ以ä¸ç±»:
1). AFHTTPRequestOperationManagerï¼å é¨å°è£ çæ¯ NSURLConnection, è´è´£åéç½ç»è¯·æ±, 使ç¨æå¤çä¸ä¸ªç±»ã(3.0åºå¼)
2). AFHTTPSessionManagerï¼å é¨å°è£ æ¯ NSURLSession, è´è´£åéç½ç»è¯·æ±,使ç¨æå¤çä¸ä¸ªç±»ã
3). AFNetworkReachabilityManagerï¼å®æ¶çæµç½ç»ç¶æçå·¥å ·ç±»ãå½åçç½ç»ç¯å¢åçæ¹åä¹å,è¿ä¸ªå·¥å ·ç±»å°±å¯ä»¥æ£æµå°ã
4). AFSecurityPolicyï¼ç½ç»å®å ¨çå·¥å ·ç±», 主è¦æ¯é对 HTTPS æå¡ã
5). AFURLRequestSerializationï¼åºååå·¥å ·ç±»,åºç±»ãä¸ä¼ çæ°æ®è½¬æ¢æJSONæ ¼å¼
(AFJSONRequestSerializer).使ç¨ä¸å¤ã
6). AFURLResponseSerializationï¼ååºååå·¥å ·ç±»;åºç±».使ç¨æ¯è¾å¤:
7). AFJSONResponseSerializer; JSON解æå¨,é»è®¤ç解æå¨.
8). AFHTTPResponseSerializer; ä¸è½è§£æå¨; JSONåXMLä¹å¤çæ°æ®ç±»å,ç´æ¥è¿åäºè¿å¶æ°æ®.对æå¡å¨è¿åçæ°æ®ä¸åä»»ä½å¤ç.
9). AFXMLParserResponseSerializer; XML解æå¨;
å¦ä½å¤çnsurlconnectionè·å¾çdata
ç¨(NSURLConnection *)connectionåºåï¼å®ååNSURLConnectionçæååéï¼æ ¹æ®å®ååºåï¼
NSMutableURLRequestå¦ä½è®¾ç½®headerï¼
第ä¸æ¥ï¼
让类å®ç°è¿ä¸¤ä¸ªæ¥å£:
NSURLConnectionDelegate,NSURLConnectionDataDelegate
第äºæ¥ï¼
å®ç°æ¥å£å¯¹åºçæ¹æ³ï¼
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
NSInteger code=[(NSHTTPURLResponse*)response statusCode];
NSLog(@"didReceiveResponse....code=%d",code);
}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{
NSLog(@"didFailWithError...%@",[error description]);
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection{
NSLog(@"connectionDidFinishLoading....");
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
NSString *str=[[NSString alloc]initWithData:data encoding:NSASCIIStringEncoding];
NSLog(@"didReceiveData....data=%@",str);
}
第ä¸æ¥ï¼
设置NSMutableURLRequeståå起请æ±ï¼
NSString *strUrl=[NSString stringWithFormat:@" ä½ çå°å"];
NSURL *url=[NSURL URLWithString:strUrl];
NSMutableURLRequest *request=[[NSMutableURLRequest alloc]initWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:];
[request setHTTPMethod:@"GET"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];//è¿ä¸ªå¾å ³é®ï¼ä¸å®è¦è®¾ç½®
[request setValue:@"å段对åºçå¼1" forHTTPHeaderField:@"ä½ å¤´é¨çå段1"];ï¼ï¼è¿éå°±æ¯ä½ èªå·±å¯¹åºçåæ°
[request setValue:@"å段对åºçå¼2" forHTTPHeaderField:@"ä½ å¤´é¨çå段2"];ï¼ï¼è¿éå°±æ¯ä½ èªå·±å¯¹åºçåæ°
mConn=[[NSURLConnection alloc]initWithRequest:request delegate:self startImmediately:true];
[mConn start];