iOS Development Network Chapter-get requests and POST requests
One,get request and POST request simple description
Create a GET request
1// 1. Set 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 Request object 6 nsurlrequest *request=[nsurlrequest requestwithurl:url];7 8// 3. Send Request
Server:
Create a POST request
1 //1. Set Request path 2 nsurl *url=[nsurl urlwithstring:@ "Http://192.168.1.53:8080/MJServer/login"];//do not need to pass parameter 3 4// 2. Create Request object 5 nsmutableurlrequest *request=[nsmutableurlrequest requestwithurl:url];//default to get request 6 request.timeoutinterval=5.0;//Set Request Timeout is 5 seconds 7 [email protected] "POST";//Set Request Method 8 9 //Set request body NSString *param=[nsstring stringwithformat:@ "username=%@&pwd=%@", self.username.text,self.pwd.text];11 // The concatenation of the string into data, set the request body . Httpbody=[param datausingencoding:nsutf8stringencoding];13 // 3. Send 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 //1. Set Request path 2 nsurl *url=[nsurl urlwithstring:@ "Http://192.168.1.53:8080/MJServer/login"];//do not need to pass parameter 3 4// 2. Create Request object 5 nsmutableurlrequest *request=[nsmutableurlrequest requestwithurl:url];//default to get request 6 request.timeoutinterval=5.0;//Set Request Timeout is 5 seconds 7 [email protected] "POST";//Set Request Method 8 9 //Set Request body NSString *param=[nsstring stringwithformat:@ "username=%@&pwd=%@", self.username.text,self.pwd.text];11 / /Convert the stitched string to data and set the request body to Httpbody=[param datausingencoding:nsutf8stringencoding];13 //client type, can only be written 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// 1. Set Request path 2 nsstring *urlstr=[nsstring stringwithformat:@ "Http://192.168.1.53:8080/MJServer/login? username=%@&pwd=%@ ", self.username.text,self.pwd.text];3 //transcoding 4 urlstr= [urlstr Stringbyaddingpercentescapesusingencoding:nsutf8stringencoding];5 Nsurl *url=[nsurl URLWithString:urlStr];6 7// 2. Create Request object 8 nsurlrequest *request=[nsurlrequest Requestwithurl:url];
Debug View:
Server:
iOS Development Network Chapter-get requests and POST requests