1. synchronous requests can request data from the Internet. Once a synchronous request is sent,ProgramUser interaction stops until the server returns data,
2. asynchronous requests do not block the main thread, but create a new thread to operate. After the user sends an asynchronous request, the user can still operate the UI, and the program can continue to run.
3. For a GET request, write the parameters directly in the access path. The operation is simple, but it is easy to see by the outside world. The security is not high, and the address can be up to 255 bytes;
4. Put the parameters in the body in the POST request. POST request operations are relatively complex. Parameters and addresses must be separated. However, they are highly secure and placed in the body, which is not easy to capture.
1. synchronous GET request
// Step 1: Create a URL
Nsurl * url = [nsurl urlwithstring: @ "http://api.hudong.com/iphonexml.do? Type = focus-c "];
// Step 2: create a network request through URL
Nsurlrequest * request = [[nsurlrequest alloc] initwithurl: URL cachepolicy: nsurlrequestuseprotocolcachepolicy timeoutinterval: 10];
// The first parameter of the nsurlrequest initialization method: Request access path, second parameter: Cache Protocol, and third parameter: Network request timeout (seconds)
The cache Protocol is an enumeration type that includes:
Nsurlrequestuseprotocolcachepolicy (Basic Policy)
Nsurlrequestreloadignoringlocalcachedata (ignore local cache)
Nsurlrequestreturncachedataelseload)
Nsurlrequestreturncachedatadontload)
Nsurlrequestreloadignoringlocalandremotecachedata (regardless of any cache policy, whether local or remote, it is always downloaded from the original address)
Nsurlrequestreloadrevalidatingcachedata (if the local cache is valid, it will not be downloaded. Otherwise, it will be downloaded again from the original address)
// Step 3: connect to the server
Nsdata * received = [nsurlconnection sendsynchronousrequest: Request returningresponse: Nil error: Nil];
Nsstring * STR = [[nsstring alloc] initwithdata: encoded ed encoding: nsutf8stringencoding];
Nslog (@ "% @", STR );
//CodeFragment from: http://www.sharejs.com/codes/objectc/5873
2. synchronous POST request
// Step 1: Create a URL
Nsurl * url = [nsurl urlwithstring: @ "http://api.hudong.com/iphonexml.do"];
// Step 2: Create a request
Nsmutableurlrequest * request = [[nsmutableurlrequest alloc] initwithurl: URL cachepolicy: nsurlrequestuseprotocolcachepolicy timeoutinterval: 10];
[Request sethttpmethod: @ "Post"]; // sets the Request Method to post. The default value is get.
Nsstring * STR = @ "type = focus-c"; // set parameters
Nsdata * Data = [STR datausingencoding: nsutf8stringencoding];
[Request sethttpbody: Data];
// Step 3: connect to the server
Nsdata * received = [nsurlconnection sendsynchronousrequest: Request returningresponse: Nil error: Nil];
Nsstring * str1 = [[nsstring alloc] initwithdata: encoded ed encoding: nsutf8stringencoding];
Nslog (@ "% @", str1 );
// The code snippet comes from: http://www.sharejs.com/codes/objectc/5873
3, asynchronous GET request
// step 1, create URL
nsurl * url = [nsurl urlwithstring: @ "http://api.hudong.com/iphonexml.do? Type = focus-c "];
// Step 2: Create a request
nsurlrequest * request = [[nsurlrequest alloc] initwithurl: URL cachepolicy: nsurlrequestuseprotocolcachepolicy timeoutinterval: 10];
// Step 3: connect to the server
nsurlconnection * connection = [[nsurlconnection alloc] initwithrequest: Request delegate: Self];
// The code snippet comes from: http://www.sharejs.com/codes/objectc/5873
4. asynchronous POST request
// Step 1: Create a URL
Nsurl * url = [nsurl urlwithstring: @ "http://api.hudong.com/iphonexml.do"];
// Step 2: Create a request
Nsmutableurlrequest * request = [[nsmutableurlrequest alloc] initwithurl: URL cachepolicy: nsurlrequestuseprotocolcachepolicy timeoutinterval: 10];
[Request sethttpmethod: @ "Post"];
Nsstring * STR = @ "type = focus-c ";
Nsdata * Data = [STR datausingencoding: nsutf8stringencoding];
[Request sethttpbody: Data];
// Step 3: connect to the server
Nsurlconnection * connection = [[nsurlconnection alloc] initwithrequest: Request delegate: Self];
// The code snippet comes from: http://www.sharejs.com/codes/objectc/5873
5. Proxy Method for asynchronous requests
// Call this method when receiving a response from the server
-(Void) connection :( nsurlconnection *) connection didreceiveresponse :( nsurlresponse *) Response
{
Nshttpurlresponse * res = (nshttpurlresponse *) response;
Nslog (@ "% @", [res allheaderfields]);
Self. receivedata = [nsmutabledata data];
}< br> // this method is called when the server receives data for transmission. This method is executed several times based on the data size.
-(void) connection :( nsurlconnection *) connection didreceivedata :( nsdata *) Data
{< br> [self. receivedata appenddata: Data];
}< br> // call this method after data transmission
-(void) connectiondidfinishloading :( nsurlconnection *) connection
{< br> nsstring * receivestr = [[nsstring alloc] initwithdata: Self. receivedata encoding: nsutf8stringencoding];
nslog (@ "% @", receivestr);
}< br> // any error occurs during network request (Network disconnection, connection timeout, etc.) will enter this method
-(void) connection :( nsurlconnection *) connection
didfailwitherror :( nserror *) error
{< br> nslog (@ "% @", [error localizeddescription]);
}< br> // The code snippet is from: http://www.sharejs.com/codes/objectc/5873