Demo Request Baidu Home
Code
1 Create the requested address
Nsurl *url = [Nsurl urlwithstring:@ "http://www.baidu.com"];
2 Create a Request object to tell the server some information
Nsurlrequest *request = [Nsurlrequest Requestwithurl:url];
3 Establish a network connection, send a request to the server, and receive the response returned by the server
[Nsurlconnection sendasynchronousrequest:request queue:[nsoperationqueue Mainqueue] completionHandler:^ ( Nsurlresponse *response, NSData *data, Nserror *connectionerror) {
method to execute after network request is completed
Nsurlresponse *response, the response object returned by the server
NSData *data, binary data returned by the server
Nserror *connectionerror if error, return error message
[Data writetofile:@ "/users/your user name/desktop/123.html" Atomically:yes];
Converting binary data to strings
NSString *str = [[NSString alloc] Initwithdata:data encoding:nsutf8stringencoding];
NSLog (@ "%@", str);
}];
ios-Network operation