#pragma mark-This is a private method, try not to use the property directly in the method, because generally the property is associated with the interface, we can use the attribute #pragma the Mark Post login method by the way of the parameter-(void) Loginwithpostwithname: (NSString *) userName pwd: (NSString *) pwd{//1 determine address nsurl nsstring *urlstring = [NSString StringW ithformat:@ "www.baidu.com"]; Nsurl *url = [Nsurl urlwithstring:urlstring]; 2 Build Request Nsmutableurlrequest (post requires this) nsmutableurlrequest *request = [Nsmutableurlrequest Requestwithurl:url]; Network access timeout time [request settimeoutinterval:2.0f]; 1) The Post request method, the network request is the Get method by default, so if we use the POST request, we must declare the request way. [Request sethttpmethod:@ "POST"]; 2) The data body of the POST request, the data body in the POST request, if there is Chinese, do not need to convert. Because the Atausingencoding method has already implemented transcoding. NSString *bodystr = [NSString stringwithformat:@ "username=%@&password=%@", username, pwd]; Convert nstring to NSData nsdata *body = [Bodystr datausingencoding:nsutf8stringencoding]; NSLog (@ "Body data%@", body); [Request Sethttpbody:body]; This is a non-proxy asynchronous request, and the asynchronous request does not prevent the main thread from continuing execution without waiting for the network to end. [Nsurlconnection sendasynchronousrequest:request Queue:[nsoperAtionqueue Mainqueue] completionhandler:^ (nsurlresponse *response, NSData *data, Nserror * error) {//This block code is only after the end of the network request Subsequent processing. if (data! = nil) {//accepted to nsstring *str = [[NSString alloc]initwithdata:data encoding:nsutf8stringencoding ]; NSLog (@ "%@", str); }else if (data = = Nil && Error! = nil)//does not receive data, but error is nil. Indicates that an empty data is accepted. {NSLog (@ "accept null data"); }else{NSLog (@ "%@", error.localizeddescription); Request error. } }];
The synchronization method is used in the most typical place when the user logs in: The user must log in to perform other operations. The Async method is when the network request is completed, the page is updated, and so on.
The only difference between proxy and non-proxy is in the third part of the network request, which is nsurlconnecttion ... The others are the same, and one is implemented through proxies, one is implemented by Nsurlconnection's two class methods, others are the same.
Cache Policy for Nsurlrequest:
2 Build Request Nsurlrequest //Cache side abuse// timeout length nsurlrequest *request = [Nsurlrequest requestwithurl:url cachepolicy: Nsurlrequestuseprotocolcachepolicy timeoutinterval:2.0f];
The benefit of using a caching strategy is that you can enter the page as soon as you enter the app, even without networking. In general, the default cache is available. These caches are cached in memory and need to be processed if they need to be preserved.