Web Service network communication for client and server in IOS (1)

Source: Internet
Author: User
Tags representational state transfer

When you open your phone on Sina Weibo app or the app is. Are you going to think about where these pictures and data appear on your phone? And how is it done? Good. So here's how it's implemented. How is the process.

When we look at the tweets of our followers or friends. The pictures and data are not appearing out of thin air, but are obtained by the server of Sina Weibo after your client requests to the server, and the data parsing is done on the client, and even in the operation. And these are the Web Service that we usually call .

Web Service technology is a kind of service through Web protocol, which ensures that application services of different platforms can interoperate and provide different services for client applications. There are three main types of Web Service technologies available today: REST, SOAP, and Xml-rpc. Today, the main introduction is REST Web Service.

REST is translated as "representational State transfer", "characterization" is the page that the client sees, the page jump is the transition of the State, and the client obtains the page to be displayed by requesting the URI.

REST is typically based on these existing protocols and standards, such as HTTP, URI, and XML, as well as HTML.

The rest Web Service is a Web service that uses HTTP and follows the REST principle. Requests that the Web Service use the URI to locate the resource. The data Interchange format with Web Service uses JSON and XML to be flexible. The HTTP request method supported by the Web Service, including POST, GET, put, or delete.

synchronize the request implemented by the GET method:

The previous article about JSON is local parsing. Now it's time to parse the return data from the server request, and then take a look at the main methods of Web Service requests.

-(void) startrequest{        nsstring *strurl = [[NSString alloc] Initwithformat:                        @ "requested server address"];    Nsurl *url = [Nsurl urlwithstring:[strurl urlencodedstring]; Nsurlrequest *request = [[Nsurlrequest alloc] initwithurl:url];            NSData *data  = [nsurlconnection sendsynchronousrequest:request returningresponse:nil Error:nil];    NSLog (@ "request complete ...");    Nsdictionary *resdict = [nsjsonserialization jsonobjectwithdata:data options:nsjsonreadingallowfragments Error:nil];    [Self reloadview:resdict];    }
Initwithformat: method Specifies the requested URL, which is the Web Service that the URL points to. The next line of code is to create the Nsurl object using the strURL string above, which is [strURL Urlencodedstring],strurl string and calls the Urlencodedstring method to convert the string to a URL string, in Intern When the ET is transmitted, no special characters such as Chinese can appear in the URL.

Using this method is to convert these characters to the URL encoding with a percent sign (the URL encoding for the "<" symbol is "%3c").





Web Service network communication for client and server in IOS (1)

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.