IOS network analysis-(9nsurlsession proxy Introduction), iosnsurlsession
From the very beginning, I have never understood anything, but now I know little about it. I think it's not just me. Everyone should have noticed that the proxy is almost running through IOS. So the question is, what should I say next. The title content. In the previous network series, I introduced NSURLSession. This article introduces the implementation of NSURLSession proxy.
NSURLSession is actually not difficult, but some people may say it is a bit messy. Don't worry, there will always be three aspects of the api that is not easy to implement, and I will present a "trivial" third party to you later.
* For NSURLSession, I want to write one or two more articles. Although it may not be available in actual development, we still know that native users are better at relying too much on the three parties, and the gods will become a little white *
Turning to the right, the current skills in the Code:
/// ViewController. m // CX-NSURLSession proxy overview /// Created by ma c on 16/3/21. // Copyright©2016 xubaoaichiyu. all rights reserved. // # import "ViewController. h "@ interface ViewController () <NSURLSessionDataDelegate> @ end @ implementation ViewController-(void) viewDidLoad {[super viewDidLoad];} // This Is A click screen event created for testing. -(Void) touchesBegan :( NSSet <UITouch *> *) touches withEvent :( UIEvent *) event {// proxy test NSURL * url = [NSURL URLWithString: @ "http: // localhost/login. php? Username = haha & password = 123 "]; // create a custom Session NSURLSession * session = [NSURLSession sessionWithConfiguration: [NSURLSessionConfiguration defaultSessionConfiguration] delegate: self delegateQueue: [[NSOperationQueue alloc] init]; NSURLSessionTask * task = [session dataTaskWithURL: url]; // enable the task [task resume];} # pragma mark-deleDate // receives the server response-(void) URLSession :( NSURLSession *) session dataTask :( NSURLSessionDataTask *) dataTask :( NSURLResponse *) response completionHandler :( void (^) (NSURLSessionResponseDisposition) completionHandler {// _ FUNCTION _ C language string using s NSLog (@ "% s" ,__ FUNCTION __); // allow the server to return the data completionHandler (NSURLSessionResponseAllow);} // the server may receive the returned data multiple times-(void) URLSession :( NSURLSession *) session dataTask :( NSURLSessionDataTask *) dataTask didReceiveData :( NSData *) data {NSLog (@ "% @", [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding]);} // request success or failure-(void) URLSession :( NSURLSession *) session didBecomeInvalidWithError :( NSError *) error {NSLog (@ "% @", error);} @ end