# 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];}