IOS network analysis-(three NSURLConnection proxy), iosnsurlconnection
For the current iOS development, the usage is simple. The oldest, classic, and direct NSURLConnection does not play a great role. However, as an ios developer, we should have a learning-loving heart, the following code briefly introduces NSURLConnection.
/// ViewController. m // CX-NSURLConnection // Created by ma c on 16/3/17. // Copyright©2016 xubaoaichiyu. all rights reserved. // # import "ViewController. h "@ interface ViewController () <NSURLConnectionDataDelegate> @ property (nonatomic, strong) NSMutableData * data; @ end @ implementation ViewController # pragma mark-life-(void) viewDidLoad {[super viewDidLoad]; // data initialization, this is very simple, but it is easy to cause unnecessary troubles if you are careless. data = [NSMutableData data]; NSString * urlString = [NSString stringWithFormat: @ "http :// Localhost/tupian.jpg "]; // if there is a Chinese character, it cannot run normally, so it is processed here. // Note: Set ios to urlString = [urlString encoding: NSUTF8StringEncoding]; NSURL * url = [NSURL URLWithString: urlString]; NSURLRequest * request = [[NSURLRequest alloc] initWithURL: url cachePolicy: NSURLRequestUseProtocolCachePolicy timeoutInterval: 15]; NSURLConnection * connection = [NSURLConnection connectionWithRequest: request delegate: self]; // enable the network [connection start] ;}# Pragma mark-deleDate // The server returns a message-(void) connection :( NSURLConnection *) connection didReceiveResponse :( NSURLResponse *) response {NSLog (@ "server returns a message ");} // accept data (multiple calls)-(void) connection :( NSURLConnection *) connection didReceiveData :( NSData *) data {[self. data appendData: data];} // request completed-(void) connectionDidFinishLoading :( NSURLConnection *) connection {NSLog (@ "% @", self. data); NSLog (@ "request completed");} // The ordered binary code is too long and part of the result is truncated. /* 21:15:15. 699 CX-NSURLConnection [3175: 173721] the server returns the message 21:15:15. 702 CX-NSURLConnection [3175: 173721] <0000000000a46 49460001 01000048 00480000 ffe10058 45786966 00004d4d 002a0000 00080002 01120003 00000001 00010000 87690004 00000001 00000026 00000000 0003a001 00030000 00010001 0000a002 00040000 00010000 02eea003 00040000 00010000 000000000000c8a70cd5 08ffd9> 21:15:15. 788 CX-NSURLConnection [3175: 173721] request completed */@ end