Since the last article on the network of Judgment, then this article on the network request it, if not, please correct
Request method: GET, POST, SOAP
Get-> Building Immutable Request objects
1. Building a network resource path
Nsurl *url = [Nsurl urlwithstring:@ "http://www.baidu.com"];
2. Build the Request object
Nsurlrequest *request = [Nsurlrequest Requestwithurl:url];
3.1 Building Connection Objects (synchronization: will cause the interface to feign animation)
NSData *data = [nsurlconnection sendsynchronousrequest:request returningresponse:&response error:&error];
3.2 Building a Connection object (async: Multithreading) Recommended usage
Nsurlconnection *conn = [[Nsurlconnection alloc] initwithrequest:request delegate:self];
[Conn start];
Implementing the Nsurlconnectiondatadelegate protocol approach
The response header has been received
-(void) connection: (Nsurlconnection *) connection didreceiveresponse: (Nsurlresponse *) response
Start receiving data: May be called multiple times
-(void) connection: (Nsurlconnection *) connection didreceivedata: (NSData *) data
Connection failed
-(void) connection: (Nsurlconnection *) connection didfailwitherror: (Nserror *) error
The connection is complete
-(void) connectiondidfinishloading: (nsurlconnection *) connection
Post-> Building a mutable Request object
1 How to set the request
[Request sethttpmethod:@ "POST"];
2 Setting the Request content
NSString *body = [NSString stringwithformat:@ "mobilecode=%@&userid=", Self.txtPhone.text];
NSData *bodydata = [Body datausingencoding:nsutf8stringencoding];
[Request Sethttpbody:bodydata];
3 Setting the request header
NSString *bodylen = [NSString stringwithformat:@ "%i", [bodydata length]];
[Request addvalue:@ "application/x-www-form-urlencoded" forhttpheaderfield:@ "Content-type"];
[Request Addvalue:bodylen forhttpheaderfield:@ "Content-length"];
Build Connection object Ditto, don't go into it!
Requests for IOS Networks