IOS development network-GET request and POST request, iosget request post

Source: Internet
Author: User

IOS development network-GET request and POST request, iosget request post

IOS development network-GET request and POST request

I,GET request and POST request

Create GET request

1 // 1. Set the Request Path 2 NSString * urlStr = [NSString stringWithFormat: @ "http: // 192.168.1.53: 8080/MJServer/login? Username = % @ & pwd = % @ ", self. username. text, self. pwd. text]; 3 NSURL * url = [NSURL URLWithString: urlStr]; 4 5 // 2. create a request object 6 NSURLRequest * request = [NSURLRequest requestWithURL: url]; 7 8 // 3. send request

Server:

Create POST request

1 // 1. set Request Path 2 NSURL * URL = [NSURL URLWithString: @ "http: // 192.168.1.53: 8080/MJServer/login"]; // parameter 3 4 // 2 is not required. create a request object 5 NSMutableURLRequest * request = [NSMutableURLRequest requestWithURL: URL]; // The default value is get request 6 request. timeoutInterval = 5.0; // set the request timeout to 5 seconds and 7 requests. HTTPMethod = @ "POST"; // Set Request Method 8 9 // Set Request body 10 NSString * param = [NSString stringWithFormat: @ "username = % @ & pwd = % @", self. username. text, self. pwd. text]; 11 // convert the concatenated string to data and set the request body to 12 request. HTTPBody = [param dataUsingEncoding: NSUTF8StringEncoding]; 13 14 // 3. send request

Server:

Ii. Comparison

Suggestion:POST requests must be used to submit users' private data.

For POST requests, all parameters of GET requests are directly exposed in URLs. The request URLs are generally recorded in server access logs, server access logs are one of the key targets of hacker attacks.

Users' private data, such as logon passwords and bank accounts.

 

Iii. Use

1. Tell the server through the request header about the client type (you can modify it to spoof the server)

1 // 1. set Request Path 2 NSURL * URL = [NSURL URLWithString: @ "http: // 192.168.1.53: 8080/MJServer/login"]; // parameter 3 4 // 2 is not required. create a request object 5 NSMutableURLRequest * request = [NSMutableURLRequest requestWithURL: URL]; // The default value is get request 6 request. timeoutInterval = 5.0; // set the request timeout to 5 seconds and 7 requests. HTTPMethod = @ "POST"; // Set Request Method 8 9 // Set Request body 10 NSString * param = [NSString stringWithFormat: @ "username = % @ & pwd = % @", self. username. text, self. pwd. text]; 11 // convert the concatenated string to data and set the request body to 12 request. HTTPBody = [param dataUsingEncoding: NSUTF8StringEncoding]; 13 14 // client type, only English 15 [request setValue: @ "ios + android" forHTTPHeaderField: @ "User-Agent"];

Server:

2. Enhance Chinese Processing

Question: Chinese characters cannot be written to URLs.

In a GET request, the relevant code segment is breakpoint for verification.

In the concatenation parameter of the string, the user name uses the "text top ".

After the URL is converted into a null value.

Tip: the URL cannot contain Chinese characters.

Solution: Perform Transcoding

1 // 1. Set the Request Path 2 NSString * urlStr = [NSString stringWithFormat: @ "http: // 192.168.1.53: 8080/MJServer/login? Username = % @ & pwd = % @ ", self. username. text, self. pwd. text]; 3 // transcode 4 urlStr = [urlStr stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding]; 5 NSURL * url = [NSURL URLWithString: urlStr]; 6 7/2. create a request object 8 NSURLRequest * request = [NSURLRequest requestWithURL: url];

View debugging:

Server:

 

Related Article

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.