The HTTP protocol probably explains
HTTP protocol (Hypertext Transfer Protocol Hypertext Transfer Protocol, access to remote network resources, format is/HTTP)
Probably explained,
HTTP protocol is a stateless protocol, what does it mean that the server to respond to the client's request, immediately disconnect, this way can save transmission time
HTTP protocol request, including request line, request header, request body
Response, including status line, response header, response body (this side specific content can Baidu)
Common response Status Codes:
The most common use of the HTTP protocol is the GET and POST request methods (others, such as put,delete, are not commonly used I do not understand)
The difference between get and post requests (the biggest difference):
Get is in the form of a request URL followed by the parameters issued to the server, a number of parameters separated by &, such as Http://www.linshao.com/login?username=name&pwd=123&type= Json
Post is the parameter that is sent to the server all in the request body
HTTP protocol Send request iOS combat
First look at the GET Request method, request the XML data situation,
The idea is this:
- Get the request path URL
- Encapsulates the Request object (here you can set the request header or the request body)
- Send synchronous or asynchronous requests
Needless to say, an example of getting weather conditions based on the city name is directly on the code:
//1. Get the URL, pay attention to the problem of garbled solution, Weatherwebservice is a macro of their own definitionnsstring* urlstr = [NSString stringWithFormat:@"%@%@", weatherwebservice,[@"Beijing"Stringbyaddingpercentescapesusingencoding:nsutf8stringencoding]];//2. Encapsulate the Request object, there is no write request, because the default is a GET requestnsurlrequest* request =[Nsurlrequest Requestwithurl:[nsurl urlwithstring:urlstr];//3. Sending an asynchronous requestnsoperationqueue* Operationqueue =[[Nsoperationqueue alloc]init]; Nsurlconnection sendasynchronousrequest:request queue:operationqueue Completionhandler:^ (Nsurlresponse *response, NSData *data, Nserror *connectionerror) { if(data) {//assign a value when the data is put back_respdata =data; [Self Performselectoronmainthread: @selector (fun) Withobject:nil Waituntildone:nil]; //NSLog (@ "%@", [[NSString Alloc]initwithdata:_respdata encoding:nsutf8stringencoding]); } }];
#import "GDataXMLNode.h" //Google provides third-party library parsing XMLgdataxmldocument* doc = [[Gdataxmldocument alloc]initwithdata:_respdata options:0Error:nil]; Gdataxmlelement*root = doc.rootelement;//Get root nodeNsarray *arr=[root Elementsforname:@"string"];
Here is how the POST request works, requesting JSON data
Ideas:
- Get the request path URL
- Encapsulates the request object, here with Nsmutableurlrequest (the subclass of Nsurlrequest)
- Send a synchronous or asynchronous request
(To do ...) This side first provides a thought, subsequent will debug the successful code to send up
Summary: Now most of the data transfer between the company and the server is using the HTTP protocol, JSON to transfer
If it happens to you, you can encapsulate a class that is specifically responsible for packaging JSON, transmitting JSON, parsing JSON, and using it as simply as possible.
Eg:sendhttpmessage: (nsstring*) message type: (nsstring*) Type URL: (nsstring*) URL;
iOS Learning Note--0003 (iOS HTTP protocol network transfer)