IOS network resolution

Source: Internet
Author: User

IOS network resolution
Synchronous and asynchronous network resolution

/* ------------------------ Get synchronization -------------------------------------*/

-(IBAction) GET_TB :( id) sender

{

// 1. Create a url

NSURL * url = [NSURL URLWithString: @ "http://project.lanou3g.com/teacher/yihuiyun/lanouproject/activitylist.php"];

// 2. Create a request object

NSMutableURLRequest * requst = [NSMutableURLRequest requestWithURL: url];

// Request Method

[Requst setHTTPMethod: @ "get"];

NSURLResponse * response = nil;

NSError * err = nil;

// 3. Establish a connection

NSData * data = [NSURLConnection sendSynchronousRequest: requst returningResponse: & response error: & err];

NSLog (@ "% @", data );

NSLog (@ "Synchronize ");


}

/* --------------------------- Get asynchronous ------------------------------------*/


-(IBAction) GET_YB :( id) sender

{

// 1. Create a url

NSURL * url = [NSURL URLWithString: @ "http://project.lanou3g.com/teacher/yihuiyun/lanouproject/activitylist.php"];

// 2. Create a request object

NSURLRequest * requset = [NSURLRequest requestWithURL: url];

// 3. Establish a connection

[NSURLConnection sendAsynchronousRequest: requset queue: [NSOperationQueue mainQueue] completionHandler: ^ (NSURLResponse * response, NSData * data, NSError * connectionError ){

NSLog (@ "% @", data );

}];

NSLog (@ "Asynchronous ");

}


/* ------------------------------ Post synchronization ------------------------------------------*/


-(IBAction) POST_TB :( id) sender

{

// 1. Create a URL

NSURL * url = [NSURL URLWithString: @ "http://ipad-bjwb.bjd.com.cn/DigitalPublication/publish/Handler/APINewsList.ashx"];

// 2. Create a request object

NSMutableURLRequest * request = [NSMutableURLRequest requestWithURL: url];

[Request setHTTPMethod: @ "post"];

// 3. Add a package

NSString * str = @ "date = 20131129 & startRecord = 5 & len = 5 & udid = 1234567890 & terminalType = Iphone & cid = 215 ";

// Convert the string to NSDATA format

NSData * dataBody = [str dataUsingEncoding: NSUTF8StringEncoding];

// 4. Set the body for the request

[Request setHTTPBody: dataBody];

// 5. Create a connection

NSData * data = [NSURLConnection sendSynchronousRequest: request returningResponse: nil error: nil];

NSLog (@ "% @", data );

NSLog (@ "POST synchronization ");

}

/* ------------------------------ Post asynchronous ------------------------------------------*/


-(IBAction) POST_YB :( id) sender

{

// 1. Create a URL

NSURL * url = [NSURL URLWithString: @ "http://ipad-bjwb.bjd.com.cn/DigitalPublication/publish/Handler/APINewsList.ashx"];

// 2. Create a request object

NSMutableURLRequest * request = [NSMutableURLRequest requestWithURL: url];

[Request setHTTPMethod: @ "post"];

// 3. Add the body to the request object

NSString * bodyStr = @ "date = 20131129 & startRecord = 5 & len = 5 & udid = 1234567890 & terminalType = Iphone & cid = 215 ";

NSData * dataBody = [bodyStr dataUsingEncoding: NSUTF8StringEncoding];

[Request setHTTPBody: dataBody];

// 4. Create a connection

[NSURLConnection sendAsynchronousRequest: request queue: [NSOperationQueue mainQueue] completionHandler: ^ (NSURLResponse * response, NSData * data, NSError * connectionError ){

NSLog (@ "% @", data );

}];

NSLog (@ "POST asynchronous ");

}

/* ------------------------ Get synchronization --------------------------------------- */-(IBAction) GET_TB :( id) sender {// 1. create url NSURL * url = [NSURL URLWithString: @ "http://project.lanou3g.com/teacher/yihuiyun/lanouproject/activitylist.php"]; // 2. create a request object NSMutableURLRequest * requst = [NSMutableURLRequest requestWithURL: url]; // request method [requst setHTTPMethod: @ "get"]; NSURLResponse * response = nil; NSError * err = nil; // 3. establish a connection NSData * data = [NSURLConnection sendSynchronousRequest: requst returningResponse: & response error: & err]; NSLog (@ "% @", data ); NSLog (@ "synchronous");}/* --------------------------- get asynchronous -------------------------------------- */-(IBAction) GET_YB :( id) sender {// 1. create url NSURL * url = [NSURL URLWithString: @ "http://project.lanou3g.com/teacher/yihuiyun/lanouproject/activitylist.php"]; // 2. create a request object NSURLRequest * requset = [NSURLRequest requestWithURL: url]; // 3. establish connection [NSURLConnection sendAsynchronousRequest: requset queue: [NSOperationQueue mainQueue] completionHandler: ^ (NSURLResponse * response, NSData * data, NSError * connectionError) {NSLog (@ "% @", data) ;}]; NSLog (@ "Asynchronous") ;}/ * ------------------------------ post synchronous handler */-(IBAction) POST_TB :( id) sender {// 1. create url nsurl * url = [NSURL URLWithString: @ "http://ipad-bjwb.bjd.com.cn/DigitalPublication/publish/Handler/APINewsList.ashx"]; // 2. create a request object NSMutableURLRequest * request = [NSMutableURLRequest requestWithURL: url]; [request setHTTPMethod: @ "post"]; // 3. add the package NSString * str = @ "date = 20131129 & startRecord = 5 & len = 5 & udid = 1234567890 & terminalType = Iphone & cid = 215 "; // convert the string to NSDATA format NSData * dataBody = [str dataUsingEncoding: NSUTF8StringEncoding]; // 4. set the request body [request setHTTPBody: dataBody]; // 5. create a connection NSData * data = [NSURLConnection sendSynchronousRequest: request returningResponse: nil error: nil]; NSLog (@ "% @", data); NSLog (@ "POST synchronization ");} /* ------------------------------ post asynchronous -------------------------------------------- */-(IBAction) POST_YB :( id) sender {// 1. create url nsurl * url = [NSURL URLWithString: @ "http://ipad-bjwb.bjd.com.cn/DigitalPublication/publish/Handler/APINewsList.ashx"]; // 2. create a request object NSMutableURLRequest * request = [NSMutableURLRequest requestWithURL: url]; [request setHTTPMethod: @ "post"]; // 3. add body NSString * bodyStr = @ "date = 20131129 & startRecord = 5 & len = 5 & udid = 1234567890 & terminalType = Iphone & cid = 215" to the request object "; NSData * dataBody = [bodyStr dataUsingEncoding: NSUTF8StringEncoding]; [request setHTTPBody: dataBody]; // 4. create connection [NSURLConnection sendAsynchronousRequest: request queue: [NSOperationQueue mainQueue] completionHandler: ^ (NSURLResponse * response, NSData * data, NSError * connectionError) {NSLog (@ "% @", data) ;}]; NSLog (@ "POST asynchronous ");}


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.