iOS使用NSURLConnection发送同步和异步HTTP Request
生活随笔
收集整理的這篇文章主要介紹了
iOS使用NSURLConnection发送同步和异步HTTP Request
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
1. 同步發(fā)送 ? - (NSString *)sendRequestSync { ????// 初始化請(qǐng)求, 這里是變長(zhǎng)的, 方便擴(kuò)展 ????NSMutableURLRequest *request = [[NSMutableURLRequest alloc]?init]; ? ????// 設(shè)置 ????[request setURL:[NSURL URLWithString:urlStr]]; ????[request setHTTPMethod:@"POST"]; ????[request setValue:host?forHTTPHeaderField:@"Host"]; ????NSString *contentLength?= [NSString stringWithFormat:@"%d", [content length]]; ????[request setValue:contentLength?forHTTPHeaderField:@"Content-Length"]; ????[request setHTTPBody:content]; ? ????// 發(fā)送同步請(qǐng)求, data就是返回的數(shù)據(jù) ????NSError *error = nil; ????NSData *data = [NSURLConnection?sendSynchronousRequest:request?returningResponse:nilerror:&error]; ????if (data == nil) { ????????NSLog(@"send request failed: %@", error); ????????return nil; ????} ? ????NSString *response = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; ????NSLog(@"response: %@", response); ????return response; } ? 2.異步發(fā)送 ? 1) 使用delegate的方式: ? - (void)sendRequestAsync { ???// 初始化請(qǐng)求 ????NSMutableURLRequest ?*request = [[NSMutableURLRequest alloc] init]; ? ????// 設(shè)置 ????[request setURL:[NSURL URLWithString:urlStr]]; ????[request setCachePolicy:NSURLRequestUseProtocolCachePolicy]; // 設(shè)置緩存策略 ????[request setTimeoutInterval:5.0]; // 設(shè)置超時(shí) ? ????//...... ? ????receivedData?= [[NSMutableData alloc] initData: nil]; ????NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request?????delegate:self]; ????if (connection == nil) { ????????// 創(chuàng)建失敗 ????????return; ????} } ? 異步發(fā)送使用代理的方式, 需要實(shí)現(xiàn)以下delegate接口: ? // 收到回應(yīng) - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { ??? ????NSLog(@"receive the response"); ???// 注意這里將NSURLResponse對(duì)象轉(zhuǎn)換成NSHTTPURLResponse對(duì)象才能去 ???NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*)response; ???if ([response respondsToSelector:@selector(allHeaderFields)]) { ???????NSDictionary *dictionary = [httpResponse allHeaderFields]; ???????NSLog(@"allHeaderFields: %@",dictionary); ???} ???[receivedData setLength:0]; } ??? ? // 接收數(shù)據(jù) ?? - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data ??? { ????NSLog(@"get some data"); ????[receivedData appendData:data]; ??? } ? //?數(shù)據(jù)接收完畢 - (void)connectionDidFinishLoading:(NSURLConnection *)connection ??? { ????NSString *results = [[NSString alloc] ????????????????????????initWithBytes:[receivedData bytes] ????????????????????????length:[receivedData length] ????????????????????????encoding:NSUTF8StringEncoding]; ???NSLog(@"connectionDidFinishLoading: %@",results); } ? // 返回錯(cuò)誤 -(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error ??? {??? ????NSLog(@"Connection failed: %@", error); ??????? } ??? 2) iOS 5.0版本新增異步發(fā)送接口: + (void)sendAsynchronousRequest:(NSURLRequest *)request ?????????????????????????queue:(NSOperationQueue*) queue ?????????????completionHandler:(void (^)(NSURLResponse*, NSData*, NSError*)) handlerNS_AVAILABLE(10_7, 5_0);
轉(zhuǎn)載于:https://www.cnblogs.com/zhwl/archive/2013/01/25/2876473.html
總結(jié)
以上是生活随笔為你收集整理的iOS使用NSURLConnection发送同步和异步HTTP Request的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Horspool 字符串快速查找算法
- 下一篇: workaround for %33 t