IOS network analysis-(9nsurlsession proxy Introduction), iosnsurlsession

Source: Internet
Author: User

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

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.