IOS network analysis-(three NSURLConnection proxy), iosnsurlconnection

Source: Internet
Author: User

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

 

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.