One,get request and POST request simple description
Create a GET request
1 //1. Set the request path2NSString *urlstr=[nsstring stringWithFormat:@"http://192.168.1.53:8080/mjserver/login?username=%@&pwd=%@", Self.username.text,self.pwd.text];3Nsurl *url=[Nsurl Urlwithstring:urlstr];4 5 //2. Create a Request object6Nsurlrequest *request=[Nsurlrequest Requestwithurl:url];7 8 //3. Sending the request
Server:
Create a POST request
1 //1. Set the request path2Nsurl *url=[nsurl urlwithstring:@"Http://192.168.1.53:8080/MJServer/login"];//no need to pass parameters3 4 //2. Create a Request object5Nsmutableurlrequest *request=[nsmutableurlrequest Requestwithurl:url];//default is GET request6Request.timeoutinterval=5.0;//Set request timeout to 5 seconds7Request. Httpmethod=@"POST";//Set Request Method8 9 //set the request bodyTenNSString *param=[nsstring stringWithFormat:@"username=%@&pwd=%@", Self.username.text,self.pwd.text]; One //convert the stitched string to data, set the request body ARequest. 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 //1. Set the request path2Nsurl *url=[nsurl urlwithstring:@"Http://192.168.1.53:8080/MJServer/login"];//no need to pass parameters3 4 //2. Create a Request object5Nsmutableurlrequest *request=[nsmutableurlrequest Requestwithurl:url];//default is GET request6Request.timeoutinterval=5.0;//Set request timeout to 5 seconds7Request. Httpmethod=@"POST";//Set Request Method8 9 //set the request bodyTenNSString *param=[nsstring stringWithFormat:@"username=%@&pwd=%@", Self.username.text,self.pwd.text]; One //convert the stitched string to data, set the request body ARequest. httpbody=[param datausingencoding:nsutf8stringencoding]; - - //client type, can only write in English the[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 the request path2NSString *urlstr=[nsstring stringWithFormat:@"http://192.168.1.53:8080/mjserver/login?username=%@&pwd=%@", Self.username.text,self.pwd.text];3 // transcoding4Urlstr=[Urlstr stringbyaddingpercentescapesusingencoding:nsutf8stringencoding];5Nsurl *url=[Nsurl Urlwithstring:urlstr];6 7 //2. Create a Request object8Nsurlrequest *request=[nsurlrequest Requestwithurl:url];
Debug View:
Server:
Description: use nsurlsession to send get and post requests please refer to the latest blog post://www.cnblogs.com/zengshuilin/p/5761531.html
iOS Development Network chapter-Send get and POST requests (using Nsurlconnection)