Get is the data that is fetched from the server, and post is the data that is sent to the server. For the Get method, the server side uses requset.querystring to get the value of the variable, and for post, the server side uses Request.Form to get the submitted data. Get is the URL where the parameter data queue is added to the Action property of the submission form, and the value is corresponding to the field one by one in the form, which can be seen in the URL. Post is the HTTP post mechanism that places the fields within the form with their contents in the HTML header, along with the URL address referred to by the Action property. The user does not see the process.
Get security is very low, post security is high, but the Get method performs better than post.
So it is recommended to use get to do the query, use post to add, delete, change.
In the 24th session of the development War, the way we use synchronization to get weather information is a get-way synchronous request, see the following code:
Import Uikitclass Viewcontroller:uiviewcontroller { @IBAction func Showweatherjson (Sender:uibutton) { // Create URL var url:nsurl! = Nsurl (string: "http://www.weather.com.cn/adat/sk/101010100.html") //Create Request object var Urlrequest:nsurlrequest = Nsurlrequest (Url:url, CachePolicy:NSURLRequestCachePolicy.UseProtocolCachePolicy, TIMEOUTINTERVAL:10) //Create Response object var response:nsurlresponse? Create Error Object var error:nserror? Make a request var data:nsdata? = Nsurlconnection.sendsynchronousrequest (URLRequest, Returningresponse: &response, Error: &error) if error! = Nil { println (error?. Code) println (error?). Description) } else { var jsonstring = NSString (data:data!, encoding:nsutf8stringencoding) println ( jsonstring)}}}
When creating Nsurlrequest objects here, a more complex constructor is used:
URL parameter: Request path
CachePolicy parameter: Cache protocol
timeOutInterval parameter: Network request time-out (units: seconds)
The cache protocol is an enumeration type:
Enum Nsurlrequestcachepolicy:uint {case useprotocolcachepolicy//base policy case reloadignoringlocalcachedata// Ignore local cache case Reloadignoringlocalandremotecachedata//unimplemented ignore any cache policy and always re-download case from source address Returncachedataelseload//First use cache, no local cache, download case with source address returncachedatadontload//Use local cache, never download, if there is no local cache, the request fails, This policy is mostly used for offline operations case Reloadrevalidatingcachedata//unimplemented If the local cache is valid, do not download, and any other situation is re-downloaded from the source address}
Here's how asynchronous get requests are:
Func Asynchronousget () { //Create Nsurl object var url:nsurl! = Nsurl (string: "<span style=" font-family:arial, Helvetica, Sans-serif; " >http://api.hudong.com/iphonexml.do?type=focus-c</span><span style= "Font-family:arial, Helvetica, Sans-serif; " > ")//parameter write directly to the path </span> //Create Request object var urlrequest:nsurlrequest = nsurlrequest (Url:url, CachePolicy: Nsurlrequestcachepolicy.useprotocolcachepolicy, Timeoutinterval:10) //Network Connection object var conn:nsurlconnection? = Nsurlconnection (Request:urlrequest, delegate:self) }
After the server is connected, the following two asynchronous request Agent method Nsurlconnectiondatadelegate is used to receive the relevant data.
Func connection (connection:nsurlconnection, Didreceiveresponse response:nsurlresponse) { //Receive response } var Jsondata:nsmutabledata = Nsmutabledata () func connection (connection:nsurlconnection, Didreceivedata data:nsdata { //received data, this method executes several times according to the data size jsondata.appenddata
The synchronous post method is as follows, the parameter is placed in the HTTP body, the security is higher
Import Uikitclass Viewcontroller:uiviewcontroller {@IBAction func Showweatherjson (Sender:uibutton) {//Create URL var url:nsurl! = Nsurl (string: "Http://api.hudong.com/iphonexml.do")//Create Request object var urlrequest:nsmutableurlrequest = nsmutable URLRequest (Url:url, CachePolicy:NSURLRequestCachePolicy.UseProtocolCachePolicy, timeoutinterval:10) urlreque St. HttpMethod = "POST"//Set Request mode is post, default is get var str:string = "Type=focus-c"//Set parameter var Data:nsdata = Str.datausin Gencoding (nsutf8stringencoding, allowlossyconversion:true)! Urlrequest.httpbody = data//Create response object var response:nsurlresponse? Create Error object var error:nserror? Make a request for Var received:nsdata? = Nsurlconnection.sendsynchronousrequest (URLRequest, Returningresponse: &response, Error: &error) I F Error! = Nil {println (error?. Code) println (error?). Description)} else {var jsonsTring = NSString (data:received!, encoding:nsutf8stringencoding) println (jsonstring)} }}
The asynchronous Post request method is as follows:
var url:nsurl! = Nsurl (string: "Http://api.hudong.com/iphonexml.do") //Create Request object var urlrequest:nsmutableurlrequest = Nsmutableurlrequest (Url:url, CachePolicy:NSURLRequestCachePolicy.UseProtocolCachePolicy, Timeoutinterval:10) Urlrequest.httpmethod = "POST"//Set Request mode is post, default is Get var str:string = "Type=focus-c"//Set parameter var data: NSData = str.datausingencoding (nsutf8stringencoding, allowlossyconversion:true)! Urlrequest.httpbody = Data var connection = nsurlconnection (Request:urlrequest, delegate:self)
You can then manipulate it in the proxy method.
Swift Language IOS8 Development Battle Note 25 network communication get and post mode