ios开发多线程、网络请求的理解 错误码的理解

    最近一直忙与项目,现在总算有点时间静下来进行一些总结了,感觉这个项目做下来,其中遇到了很多问题,一一记录下:

    1.网络请求的初步理解 IOS原生态的请求方式
       所谓网络请求,肯定首先是拿一个URL连接去请求的,所以首先从
       NSString(字符串链接)-----》NSUrl-----》NSMutableRequest----》NSUrlConnection

      代码如下:
     
- (void)viewDidLoad {
      [super viewDidLoad];
      NSString *downloadUrl = @"http://app.91.com/soft/Controller.ashx?Action=Download&id=1105334&m=81fcf80c1b7272b330382fdb5279018e";
      NSURL *url = [NSURL URLWithString:downloadUrl];
      NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
     //[NSMutableURLRequest requestWithURL:url cachePolicy:nsurlrequest timeoutInterval:10.0
     //检测初始化的request是否可用 合法
     if ([NSURLConnection canHandleRequest:request]) {
         //self.connection = [NSURLConnection connectionWithRequest:request delegate:self];        
        self.connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
        self.data = [NSMutableData data];
     }else {
      NSLog(@"request invalide");
           }
}

//可以在此方法中做一些下载进度的处理
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    [self.data appendData:data];
    NSLog(@"data lenght = %d",[data length]);
}

//获取一些响应头的信息
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
   NSLog(@"response = %@",response);
}

//请求失败获取错误信息
 - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
    //关于错误码的理解,首先这个项目中的服务端会自定义一些系统级别的错误码(如服务器内部错误、数据库错误、session过期、加密方式无效等等)和业务错误码(如请求某类产品类别的所有产品,会出现没有这个产品类别ID的错误,于是服务器就把他们定义的这个错误码返回给客户端)
   //值得一提的是,你客户端能接受到这些错误码已经就表示客户端与服务端是能正常通信的,是不会接收到网络通信故障方面的错误码的
    //另外在客户端这边也是有一些本地错误码(就是处理http等协议的错误码)的,只有当网络出现故障的时候才能接收到这些本地的(此处就会调用这个
didFailWithError函数产生本地错误码,而服务端的那些错误码是在请求成功函数connectionDidFinishLoading里接收到的),所谓本地错误码,就是这个URLConnection相关的有个NSURLError.h的文件中一个枚举定义的错误码,并且这个枚举里定义的错误码又是CFNetwork这个系统框架中CFNetworkError.h中里面定义的一些错误码,主要就是网络连接的,如http协议的、ftp的 SSL的等
    NSLog(@"error = %@",error);
    [connection release];
 }

//请求完成
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
   NSLog(@"connection");
   [connection release];
   NSLog(@"[connection retainCount] = %d",[connection retainCount]);
   NSLog(@"[self.connection retainCount] = %d",[self.connection retainCount]);
    // NSLog(@"file = %@",[[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:@"/12"]);
   //NSLog(@"self.data = %@",self.data);
   [self.data writeToFile:[[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:@"/34"] atomically:YES];
}

//上传进度
- (void)connection:(NSURLConnection *)connection didSendBodyData:(NSInteger)bytesWritten totalBytesWritten:(NSInteger)totalBytesWritten totalBytesExpectedToWrite:(NSInteger)totalBytesExpectedToWrite {
    // NSLog(@"bytesWritten = %d",bytesWritten);
    //NSLog(@"totalBytesWritten = %d",totalBytesWritten);
    // NSLog(@"totalBytesExpectedToWrite = %d",totalBytesExpectedToWrite);
    // NSLog(@"--------");
}

// NSURLConnectionDownloadDelegate委托 获取下载进度,只在ios5.0之后才支持 所以一般不会用到
- (void)connectionDidFinishDownloading:(NSURLConnection *)connection destinationURL:(NSURL *)destinationURL {
     NSLog(@"destinationURL = %@",destinationURL);
}

- (void)connection:(NSURLConnection *)connection didWriteData:(long long)bytesWritten totalBytesWritten:(long long)totalBytesWritten expectedTotalBytes:(long long)expectedTotalBytes {
    NSLog(@"bytesWritten = %lld",bytesWritten);
    NSLog(@"totalBytesWritten = %lld",totalBytesWritten);
    NSLog(@"expectedTotalBytes = %lld",expectedTotalBytes);
    NSLog(@"--------");
}

- (IBAction)cancel:(id)sender {
   [self.connection cancel];
   NSLog(@"after cancel [self.connection retainCount] = %d",[self.connection retainCount]);
}

- (void)viewDidUnload {
   [super viewDidUnload];
   // Release any retained subviews of the main view.
   // e.g. self.myOutlet = nil;
   [_connection release];
   _connection = nil;
   [_data release];
   _data = nil;
}


   上述都是异步处理的方式  NSURLConnection参考苹果官方文档:https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/nsurlconnection_Class/Reference/Reference.html
   其他参考链接:http://stm237.iteye.com/blog/1005752
               http://iosdeveloper.diandian.com/post/2011-12-15/10122381
               http://disanji.net/2011/04/23/iphone-new-request/
posted @ 2012-08-02 10:54  SEC.VIP_网络安全服务  阅读(178)  评论(0编辑  收藏  举报