iOS network programming development GET requests and post requests

Source: Internet
Author: User

iOS network programming development GET requests and post requests

One,get request and POST request simple description

Create a GET request

//     1. Set the request path    NSString *urlstr=[nsstring stringWithFormat:@ "http://192.168.1.53:8080/mjserver/login?username=%@ &pwd=%@", Self.username.text,self.pwd.text];     *url=[Nsurl urlwithstring:urlstr];     //     2. Create a Request object    Nsurlrequest *request=[nsurlrequest requestwithurl:url];     //     3. Sending the request

Server:

Create a POST request

//1. Set the request pathNsurl *url=[nsurl urlwithstring:@"Http://192.168.1.53:8080/MJServer/login"];//no need to pass parameters//2. Create a Request objectNsmutableurlrequest *request=[nsmutableurlrequest Requestwithurl:url];//default is GET requestRequest.timeoutinterval=5.0;//Set request timeout to 5 secondsRequest. Httpmethod=@"POST";//Set Request Method//set the request bodyNSString *param=[nsstring stringWithFormat:@"username=%@&pwd=%@", Self.username.text,self.pwd.text]; //convert the stitched string to data, set the request bodyRequest. httpbody=[param datausingencoding:nsutf8stringencoding]; //3. Sending the request

Server:

Second, Comparison

Recommendation: submit a user's privacy data be sure to use the POST request

Relative to a POST request, all parameters of a GET request are exposed directly to the URL, and the requested URL is typically recorded in the server's access log, and the server's access log is one of the key objects of the hacker attack

User's privacy data such as login password, bank account and so on.

Third, the use

1. Tell the server through the request header, the type of client (can be modified to spoof the server)

//1. Set the request pathNsurl *url=[nsurl urlwithstring:@"Http://192.168.1.53:8080/MJServer/login"];//no need to pass parameters//2. Create a Request objectNsmutableurlrequest *request=[nsmutableurlrequest Requestwithurl:url];//default is GET requestRequest.timeoutinterval=5.0;//Set request timeout to 5 secondsRequest. Httpmethod=@"POST";//Set Request Method//set the request bodyNSString *param=[nsstring stringWithFormat:@"username=%@&pwd=%@", Self.username.text,self.pwd.text]; //convert the stitched string to data, set the request bodyRequest. httpbody=[param datausingencoding:nsutf8stringencoding]; //client type, can only write in English[Request SetValue:@"ios+android"Forhttpheaderfield:@"user-agent"];

Server:

2. Strengthen the handling of Chinese

Problem: URL is not allowed to write Chinese

In a GET request, the relevant code snippet breaks to verify.

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

Converted to a URL, the whole becomes a null value.

Hint: The URL cannot contain Chinese.

Resolution: transcoding

//     1. Set the request path    NSString *urlstr=[nsstring stringWithFormat:@ "http://192.168.1.53:8080/mjserver/login?username=%@ &pwd=%@", Self.username.text,self.pwd.text];    // transcoding   urlstr= [Urlstr stringbyaddingpercentescapesusingencoding:nsutf8stringencoding];     *url=[Nsurl urlwithstring:urlstr];     //     2. Create a Request object    Nsurlrequest *request=[nsurlrequest Requestwithurl:url];

Debug View:

Server:

iOS network programming development GET requests and post requests

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.