Network HTTP, JSON, XML parsing and other review

Source: Internet
Author: User

One basic elements of HTTP requests
1. Request URL: Which path the client uses to locate the server

2. Request parameters: Data sent by the client to the server
* such as user name and password to be sent at login

3. Return results: Data returned by the server to the client
* Typically JSON data or XML data

II. Basic HTTP request Steps (Mobile client)
1. Stitching "Request URL" + "?" + "Request parameter"
* Format of request parameter: Parameter name = argument value
* Multiple request parameters separated by &: Parameter name 1 = parameter value 1& argument Name 2 = parameter value 2
* For example: http://localhost:8080/MJServer/login?username=123&pwd=456

2. Sending the request

3. Parsing the data returned by the server

Third, JSON parsing
1. Using Nsjsonserialization class parsing
* JSON data (NSData)--Foundation-oc objects (Nsdictionary, Nsarray, NSString, NSNumber)

+ (ID) Jsonobjectwithdata: (NSData *) Data options: (nsjsonreadingoptions) opt error: (NSERROR *) error;

2.JSON Analytic law
* {}--nsdictionary @{}
* []--Nsarray @[]
* "--nsstring @" "
*---NSNumber @10

Iv. nsurlconnection
1. Publish an asynchronous request--block callback

+ (void) Sendasynchronousrequest: (nsurlrequest*) Requestqueue: (nsoperationqueue*) Queuecompletionhandler: (void (^) (nsurlresponse* response, nsdata* data, nserror* connectionerror)) Handler

* Request: Requests that need to be sent
* Queue: Generally with the home row, storage handler this task
* Handler: When the request is complete, the block is automatically called

2. Basic steps for sending requests using nsurlconnection

// 1> Creating URLs Nsurl *url = [Nsurl urlwithstring:@ "http://4234324/5345345"]; // 2> Creating a request Nsurlrequest *request = [Nsurlrequest requestwithurl:url]; // 3> Sending requests [nsurlconnection sendasynchronousrequest:request queue:queue Completionhandler:^ (Nsurlresponse * Response, NSData *data, Nserror *connectionerror) {//4> processing the data returned by the server }];

Five, XML
1. Syntax
1> Document Declaration
<?xml version= "1.0" encoding= "UTF-8"?>

2> elements
3> Property
<videos>
<video name= "Small yellow No. 01" length= "/>"
<video name= "Small yellow No. 01" length= "/>"
</videos>
* Videos and video are elements (nodes)
* Name and length are called attributes of the element
* The video element is a child element of the videos element

2. Parsing
1> Sax parsing: Parse-by-element down, for large files
* Nsxmlparser

2> Dom parsing: A breath of loading the entire XML document into memory, suitable for small files, using the simplest
* Gdataxml

VI. Communication Process for HTTP
1. Request
1> Request Line: Request method, request path, version of HTTP protocol
Get/mjserver/resources/images/1.jpg http/1.1

2> Request Header: Some descriptive information for the client
* User-agent: Client-side environment (software environment)

3> Request Body: Post request only this thing
* Request parameters, data sent to the server

2. Response
1> status line (response line): Version of HTTP protocol, response status Code, response status description
http/1.1 OK

2> response Header: Some descriptive information about the server
* Content-type: The content type returned to the client by the server
* Content-length: The length of the content returned to the client by the server (such as the size of the file)

3> Entity content (response body)
* The server returns specific data to the client, such as file data

Vii. Request method for HTTP
1.GET
1> Features
* All request parameters are stitched behind the URL

2> Disadvantages
* All request data is exposed in the URL, not too secure
* URL is limited in length and cannot send too many parameters

3> Use occasions
* If you only request data from the server, you typically use GET requests

4> How to send a GET request
* Default is GET request

// 1.URL Nsurl *url = [Nsurl urlwithstring:@ "http://www.baidu.com"]; // 2. Request Nsurlrequest *request = [Nsurlrequest requestwithurl:url]; // 3. Sending the request [Nsurlconnection sendasynchronousrequest:request queue:[nsoperationqueue Mainqueue] completionHandler:^ ( Nsurlresponse *response, NSData *data, Nserror *connectionerror) {}];

2.POST
1> Features
* Place all request parameters in the request body (httpbody)
* Theoretically, there is no limit to the size of the data sent to the server

2> Use occasions
* In addition to requesting data from the server, you can use the POST request
* If the data sent to the server is some privacy, sensitive data, you must use the POST request

3> How to send a POST request

//1. Create a URL: request pathNsurl *url = [Nsurl urlwithstring:@"Http://localhost:8080/MJServer/login"];//2. Create a requestNsmutableurlrequest *request =[Nsmutableurlrequest Requestwithurl:url];//Set Request MethodRequest. HttpMethod =@"POST";//Set Request body: Request ParametersNSString *param = [NSString stringWithFormat:@"username=%@&pwd=%@", Usernametext, Pwdtext];//NSString-NSDataRequest. Httpbody = [param datausingencoding:nsutf8stringencoding];

Eight, the common method of Nsmutableurlrequest
1. Set timeout

5;

Nsurlrequest is not able to set the timeout because the object is immutable

Nine, url transcoding
1.URL can not contain Chinese, you have to transcode the Chinese (plus a bunch of%)

NSString *urlstr = [NSString stringWithFormat:@ "http://localhost/login?username= drink &pwd=123 "  = [Urlstr stringbyaddingpercentescapesusingencoding:nsutf8stringencoding]; // Urlstr = = @ " Http://localhost/login?username=%E5%96%9D%E5%96%9D &pwd=123 "

X. Data security

1. Network Data encryption
1> Encrypted objects: Privacy data, such as passwords, bank information
2> Encryption Scheme
* Submit privacy data, must use POST request
* Encrypt private data using cryptographic algorithms, such as MD5
3> encryption Enhancement: In order to increase the difficulty of the crack
* 2 Md5:md5 for clear text (MD5 ($pass))
* Salt the plaintext before md5:md5 ($pass. $salt)

2. Local Storage encryption
1> Encrypted objects: Important data, such as game data

3. Code Security issues
1> now has tools and techniques to decompile source code: Reverse Engineering
* The anti-compilation is pure C language, the readability is not high
* At the very least, you can know which frames are used in the source code.

2> reference book: "Reverse engineering of iOS"

3> Solution: Confusing code before publishing

// * before confusing @interface hmperson:nsobject-(void) run; -(void) eat; @end // * After confusion @interface a:nsobject-(void) A; -(void) b; @end

Xi. monitoring the status of the network
1. Active monitoring and monitoring of network status

// whether WiFi+ (BOOL) Isenablewifi {return ([[Reachability Reachabilityforlocalwifi] Currentreachabilitystatus]! = notreachable);} // whether 3G+ (BOOL) isenable3g {return ([[Reachability reachabilityforinternetconnection] Currentreachabilitystatus]! = notreachable);}

2. Monitor network status
1> Monitoring Notifications

object: nil];

2> Start listening to network status

// get reachability object self.reachability = [reachability reachabilityforinternetconnection]; // Start monitoring the network [Self.reachability Startnotifier];

3> Remove Listener

[Self.reachability Stopnotifier]; [[Nsnotificationcenter Defaultcenter] removeobserver:self];

Network HTTP, JSON, XML parsing and other review

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.