iOS Learning Note--0003 (iOS HTTP protocol network transfer)

Source: Internet
Author: User

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:

    1. Get the request path URL
    2. Encapsulates the Request object (here you can set the request header or the request body)
    3. 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:

    1. Get the request path URL
    2. Encapsulates the request object, here with Nsmutableurlrequest (the subclass of Nsurlrequest)
    3. 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)

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.