iphone网络post连接的两种处理方式(同步和异步)
2013-08-13 00:00
183 查看
第一种: 直接返回方式。
第二种,采用事件代理方式
-(void)UpadaPost:(NSString *)strcontext URL:(NSString *)urlstr{ NSLog(urlstr); NSLog(strcontext); assert(strcontext != NULL); assert(urlstr != NULL); NSData *postData = [strcontext dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]; NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]]; NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease]; [request setURL:[NSURL URLWithString:urlstr]]; [request setHTTPMethod:@"POST"]; [request setValue:postLength forHTTPHeaderField:@"Content-Length"]; [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"]; [request setHTTPBody:postData]; NSURLResponse *respone; NSError *error; NSData *myReturn =[NSURLConnection sendSynchronousRequest:request returningResponse:&respone error:error]; NSLog(@"%@", [[NSString alloc] initWithData:myReturn encoding:NSUTF8StringEncoding]); }
第二种,采用事件代理方式
-(void)UpadaPost:(NSString *)strcontext URL:(NSString *)urlstr{ NSLog(urlstr); NSLog(strcontext); assert(strcontext != NULL); assert(urlstr != NULL); NSData *postData = [strcontext dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]; NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]]; NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease]; [request setURL:[NSURL URLWithString:urlstr]]; [request setHTTPMethod:@"POST"]; [request setValue:postLength forHTTPHeaderField:@"Content-Length"]; [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"]; [request setHTTPBody:postData]; NSURLConnection *conn=[[NSURLConnection alloc] initWithRequest:request delegate:self]; if (conn) { NSLog(@"Connection success"); [UIApplication sharedApplication].networkActivityIndicatorVisible = YES; [conn retain]; } else { // inform the user that the download could not be made } } // 收到响应时, 会触发 - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { // 注意这里将NSURLResponse对象转换成NSHTTPURLResponse对象才能去 NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*)response; if ([response respondsToSelector:@selector(allHeaderFields)]) { NSDictionary *dictionary = [httpResponse allHeaderFields]; NSLog([dictionary description]); NSLog(@"%d",[response statusCode]); } } // Forward errors to the delegate. //链接错误 - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error { //[self performSelectorOnMainThread:@selector(httpConnectEnd) withObject:nil waitUntilDone:NO]; NSLog(@"%@",[error localizedDescription]); } // Called when a chunk of data has been downloaded. //接收数据 每收到一次数据, 会调用一次 - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { // Process the downloaded chunk of data. NSLog(@"%d", [data length]); //NSLog(@"%@", [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]); //[self performSelectorOnMainThread:@selector(updateProgress) withObject:nil waitUntilDone:NO]; } //接收结束 - (void)connectionDidFinishLoading:(NSURLConnection *)connection { NSLog(@"%@",connection); //NSLog(@"%lld", received_); //[self performSelectorOnMainThread:@selector(httpConnectEnd) withObject:nil waitUntilDone:NO]; // Set the condition which ends the run loop. }
相关文章推荐
- Iphone网络post连接的两种处理方式(同步和异步)
- iphone网络post连接的两种处理方式(同步和异步)(zz)
- iphone网络post连接的两种处理方式(同步和异步)
- 网络请求数据 get请求方式 post请求 协议异步连接服务器 block异步连接服务器
- 网络处理2-异步POST请求和同步请求
- 网络处理2-异步POST请求和同步请求
- 网络数据请求(同步/异步连接下的GET/POST请求)
- 网络处理2-异步POST请求和同步请求
- 网络处理2-异步POST请求和同步请求
- 网络处理2-异步POST请求和同步请求
- 网络处理(2)-异步POST请求和同步请求
- 网络处理2-异步POST请求和同步请求
- 网络处理2-异步POST请求和同步请求
- 两种HTTP连接方式POST&GET的比较
- httpURLConnection-网络请求的两种方式-get请求和post请求
- 【java面试系列之网络编程】TCP和UDP的区别、TCP协议的三次握手和四次挥手、TCP协议的通信状态、网络编程时的同步、异步、阻塞、非阻塞、进程间的通信方式、TCP的流量控制和拥塞控制
- 有关虚拟机下的 linux连接网络的两种方式 (桥接)
- 网络请求数据(同步POST,异步POST)
- UI14_GET-POST-BLOCK(网络请求的同步异步)
- iOS网络编程--NSConnection的同步连接与异步连接