Network Programming NSURLConnection and iosnsurlconnection for Ios

Source: Internet
Author: User

Network Programming NSURLConnection and iosnsurlconnection for Ios

NSURLConnection is primarily used for network access through four categories: NSURL, NSURLRequest, NSMutableURLRequest, NSURLConnection

  I. Basic Knowledge

(1) NSURL: request address

(2) NSURLRequest: encapsulates a request and stores all the data sent to the server, including an NSURL object, request method, request header, and request body ....

(3) NSMutableURLRequest: NSURLRequest subclass

(4) NSURLConnection: responsible for sending requests and establishing connections between the client and the server. Send NSURLRequest data to the server and collect response data from the server

  Ii. function implementation

  1./******** get method *******/

// Global variable, receiving data NSMutableData * requestData; NSURL * url = [NSURL URLWithString: @ "http: // 192.168.20.2/logo. php? UserName = jereh & pwd = 123 "]; // create a request NSURLRequest * request = [NSURLRequest requestWithURL: URL] through url; // connect to the server through NSURLConnection, and send the request NSURLConnection * connection = [NSURLConnection connectionWithRequest: request delegate: self]; [connection start]; // receives the request-(void) connection :( NSURLConnection *) connection didReceiveResponse :( NSURLResponse *) response {NSLog (@ "didReceiveResponse"); // initialize datarequestData = [NSMutableData data] In this method;} // receive data-(void) connection :( NSURLConnection *) connection didReceiveData :( NSData *) data {// prevent the file from being too large. append [requestData appendData: data] multiple times;} // The data is received-(void) connectionDidFinishLoading :( NSURLConnection *) connection {NSDictionary * dic = [NSJSONSerialization JSONObjectWithData: requestData options: Unknown error: nil]; NSLog (@ "% @", dic );} // transmission failed-(void) connection :( NSURLConnection *) connection didFailWithError :( NSError *) error {NSLog (@ "didFailWithError ");}

2./******* post method *******/

NSURL * url = [NSURL URLWithString: @ "http: // 192.168.20.2/loginPost. php "]; // defines the variable request NSMutableURLRequest * request = [NSMutableURLRequest requestWithURL: url]; // sets the request Method request. HTTPMethod = @ "POST"; // you can specify the request parameter. HTTPBody = [@ "userName = jereh & pwd = 123" dataUsingEncoding: NSUTF8StringEncoding]; NSURLConnection * con = [NSURLConnection connectionWithRequest: request delegate: self]; [con start];

3. Non-proxy method

// Synchronize NSURLResponse * response; NSData * data = [NSURLConnection sendSynchronousRequest: request returningResponse: & response error: nil]; // asynchronous [NSURLConnection sendAsynchronousRequest: request queue: [NSOperationQueue mainQueue] completionHandler: ^ (NSURLResponse * response, NSData * data, NSError * connectionError) {// main thread operation}];

 

Author: Jerry Education
Source: http://www.cnblogs.com/jerehedu/
Copyright Disclaimer: The copyright of this article is shared by Yantai jereh Education Technology Co., Ltd. and the blog Park. You are welcome to repost it. However, you must keep this statement without the consent of the author and provide the original article connection clearly on the article page, otherwise, you are entitled to pursue legal liability.
Technical Consultation:

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.