用NSURL NSURLRequest NSURLConnection http例子

来源:互联网 发布:阿里云换公网ip 编辑:程序博客网 时间:2024/05/18 03:13

I use NSURL to do my HTTP requests. It is pretty straightforward. You can read all about it on the NSURL Class Reference, but here is a snippet of sample code:

 

----------------------------------------------------------------------------------------------------------

// set up your request

NSURL * url = [NSURL URLWithString:@"http://www.stackoverflow.com"];

NSURLRequest * request = [NSURLRequest requestWithURL:url

    cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval:60];

 

// create your connection with your request and a delegate (in this case 

// the object making the request)

_connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];

---------------------------------------------------------------------------------------------------------
You just need to implement some delegate methods to handle the data responses

 

 

 

- (void)connection:(NSURLConnection*)connection didReceiveResponse:(NSHTTPURLResponse*)response

- (void)connection:(NSURLConnection*)connection didReceiveData:(NSData*)data

- (void)connection:(NSURLConnection*)connection didFailWithError:(NSError*)error

- (void)connectionDidFinishLoading:(NSURLConnection *)connection

原创粉丝点击