Ios network learning ------ 2 implement synchronous post requests using a non-proxy method

Source: Internet
Author: User

# Pragma mark-this is a private method. Try not to directly use attributes in the method, because attributes are generally associated with the interface, we can use the attribute # pragma mark post login method through parameters-(void) loginWithPostWithName :( NSString *) userName pwd :( NSString *) pwd {// 1 confirm the address NSURL NSString * urlString = [NSString stringWithFormat: @ "www.baidu.com"]; NSURL * url = [NSURL URLWithString: urlString]; // 2 create a request NSMutableURLRequest (this is required for post) NSMutableURLRequest * request = [NSMutableURLRequest requestWithURL: url];/ /Network access timeout [request setTimeoutInterval: 2.0f]; // 1) in post request mode, the network request is by default the get method. Therefore, if post request is used, the request method must be declared. [Request setHTTPMethod: @ "POST"]; // 2) the data body of the post request. If the data body in the post request contains Chinese characters, no conversion is required. Because the ataUsingEncoding method has implemented transcoding. NSString * bodyStr = [NSString stringWithFormat: @ "username =%@ & password =%@", userName, pwd]; // convert nstring to nsdata NSData * body = [bodyStr dataUsingEncoding: NSUTF8StringEncoding]; NSLog (@ "body data % @", body); [request setHTTPBody: body]; /// 3 create and start the connection to the nsulconnection // NSURLConnection * conn = [NSURLConnection connectionWithRequest: request delegate: self]; // [conn start]; // start the connection, this is because a network request has already occurred. This is an asynchronous connection request. After the request is sent, it is processed by the proxy. // 3. No proxy synchronization request is required. NSURLResponse * response = nil; NSError * error = nil; // second, the three parameters are pointer of the pointer, and all parameters must use the accessors, this method is synchronous. The synchronization operation is not completed, and subsequent code will not be executed. NSData * data = [NSURLConnection sendSynchronousRequest: request returningResponse: & response error: & error]; // subsequent processing of synchronously accessed data if (data! = Nil) {// receives data, indicating that the job is normal NSString * str = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding]; NSLog (@ "% @", str);} else if (data = nil & error! = Nil) // No data is received, but error is nil .. Accept NULL data. {NSLog (@ "accept empty data");} else {NSLog (@ "% @", error. localizedDescription); // request error.} /// Prepare the server notification and prepare the intermediate data // self. serverData = [NSMutableData];}

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.