Nsurl |
Request Address |
Nsurlrequest |
Encapsulates a request to save all data sent to the server |
Nsmutableurlrequest |
A subclass of Nsurlrequest that provides a way to change the properties of a request |
Nsurlconnection |
Responsible for sending the request to establish a connection between the client and the server. Send nsurlrequest data to the server and collect response data from the server |
Nsmutableurlrequest change the properties of the request as follows:
Nsurl *url = [Nsurl urlwithstring@ "http://server.com/postme"*req = [ Nsmutableurlrequest Requestwithurl:url]; [Req Sethttpmethod: @" POST " ]; [Req sethttpbody:[@ "Post body" datausingencoding:nsutf8stringencoding];
There are three main steps to sending a request using Nsurlconnection:
(1) Create a Nsurl object, set the request path;
(2) Incoming Nsurl Create a Nsurlrequest object, set the request header and the request body;
(3) Send nsurlrequest (send request) using Nsurlconnection.
(Finishing the Arrogant God: http://www.cnblogs.com/wendingding/p/3813572.html);
Sync Request |
Request data from the Internet, once the synchronization request is sent, the program will stop the user interaction until the server returns the data to complete before the next operation. |
Asynchronous request |
does not block the main thread, but creates a new thread to operate, after the user makes an asynchronous request, the UI can still be manipulated, the program can continue to run |
Get load Mode |
Write the parameters directly on the access path. Simple operation, but easy to be seen by the outside, security is not high, address up to 255 bytes; |
Post Load Mode |
Put the parameters inside the body. Post request operation is relatively complex, need to separate parameters and addresses, but high security, parameters placed inside the body, not easily captured. |
Get Asynchronous Request:
1 //The first step is to create the URL2 3Nsurl *url = [Nsurl urlwithstring:@"http://api.hudong.com/iphonexml.do?type=focus-c"];4 5 //second step, create the request6 7Nsurlrequest *request = [[Nsurlrequest alloc]initwithurl:url Cachepolicy:nsurlrequestuseprotocolcachepolicy timeOutInterval:Ten];8 9 //step three, connect the serverTen OneNsurlconnection *connection = [[Nsurlconnection alloc]initwithrequest:requestDelegate: Self];
Post Asynchronous Request:
//The first step is to create the URLNsurl*url = [Nsurl urlwithstring:@"http://api.hudong.com/iphonexml.do"]; //second step, create the requestnsmutableurlrequest*request = [[Nsmutableurlrequest alloc]initwithurl:url Cachepolicy:nsurlrequestuseprotocolcachepolicy timeOutInterval:Ten]; [Request Sethttpmethod:@"POST"]; NSString*str =@"type=focus-c"; NSData*data =[str datausingencoding:nsutf8stringencoding]; [Request Sethttpbody:data]; //step three, connect the servernsurlconnection*connection = [[Nsurlconnection alloc]initwithrequest:requestDelegate: Self];
Cache protocol (enum type)
Nsurlrequestuseprotocolcachepolicy (Base policy) Nsurlrequestreloadignoringlocalcachedata (ignoring local cache) Nsurlrequestreturncachedataelseload (use cache first, if there is no local cache, download from the original address) Nsurlrequestreturncachedatadontload (using local cache, From no load, if there is no cache locally, the request fails, and this policy is mostly used for offline operation Nsurlrequestreloadignoringlocalandremotecachedata (ignoring any caching policies, whether local or remote, Always re-download from the original address) Nsurlrequestreloadrevalidatingcachedata (if the local cache is valid, do not download, and any other cases are re-downloaded from the original address)
(Finishing the Arrogant God: http://my.oschina.net/sunqichao/blog/75011);
The HTTP status code:http status code, which is a 3-bit numeric code that represents the HTTP response status of a Web server, is defined by the RFC 2616 specification.
When the server returns data,the Nsurlconnectiondelegate proxy method :
//when the response to the server is received (the server is connected) , the- (void) Connection: (Nsurlconnection *) theconnection didreceiveresponse: (Nsurlresponse *) response{}//called when data is received from the server (may be called multiple times, only partial data is passed at a time)- (void) Connection: (Nsurlconnection *) aconn didreceivedata: (NSData *) data{}//when the server's data is loaded, it is called- (void) Connectiondidfinishloading: (Nsurlconnection *) aconn{}//Request Error (failed) call (Request timeout \ network \ No net \, usually refers to client error)- (void) Connection: (Nsurlconnection *) aconn didfailwitherror: (Nserror *) error{}
Apple's own nsjsonserialization JSON-based packet encapsulation and analysis (related third-party parsing JSON class library has touchjson,jsonkit,sbjon, etc.)
Packaging:
Nsdictionary *dic = [Nsdictionary Dictionarywithobjectsandkeys:@"value1",@"Key1",@"value2",@"Key2",@"Value3",@"Key3", nil]; //Isvalidjsonobject to determine whether an object can be built as a JSON object if([nsjsonserialization isvalidjsonobject:dic]) {Nserror*error; //create a JSON from data, nsjsonwritingprettyprinted specify the white space produced by JSON, making the output more readable. NSData *jsondata = [nsjsonserialization datawithjsonobject:dic options:nsjsonwritingprettyprinted error:&ERROR]; NSString*json =[[NSString alloc] Initwithdata:jsondata encoding:nsutf8stringencoding]; NSLog (@"JSON data:%@", JSON); }
Analytical:
Nserror *error; //loading a Nsurl objectNsurlrequest *request = [Nsurlrequest requestwithurl:[nsurl urlwithstring:@"http://m.weather.com.cn/data/101120101.html"]]; //put the requested URL data into the NSData objectNSData *response =[nsurlconnection sendsynchronousrequest:request returningresponse:nil Error:nil]; //iOS self-contained parsing class nsjsonserialization parsing data from response into a dictionaryNsdictionary *weatherdic = [nsjsonserialization jsonobjectwithdata:response options:nsjsonreadingmutableleaves Error :&ERROR]; Nsdictionary*weatherinfo = [Weatherdic objectforkey:@"Weatherinfo"]; NSString*text = [NSString stringWithFormat:@"today is%@%@%@ weather condition is:%@%@", [Weatherinfo Objectforkey:@"date_y"],[weatherinfo Objectforkey:@"Week"],[weatherinfo Objectforkey:@" City"], [Weatherinfo Objectforkey:@"Weather1"], [Weatherinfo Objectforkey:@"Temp1"]]; NSLog (@"weatherinfo:%@", text);
(Finishing the Arrogant God: http://blog.csdn.net/xyz_lmn/article/details/8968187);
A brief introduction to the network development--nsurlconnection class