Requests for IOS Networks

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.