您的位置:首页 > 理论基础 > 计算机网络

网络请求

2014-04-03 19:47 120 查看
一、NSURLConnection-同步请求网络数据

-(void)viewDidAppear:(BOOL)animated{
[super
viewDidAppear:animated];

NSLog(@"We are here...");

NSString
*urlAsString =@"http://www.yahoo.com";

NSURL *url = [NSURL
URLWithString:urlAsString];

NSURLRequest *urlRequest = [NSURLRequest
requestWithURL:url];

NSURLResponse *response = nil;

NSError *error = nil;

NSLog(@"Firing synchronous urlconnection...");

NSData *data = [NSURLConnection
sendSynchronousRequest:urlRequest
returningResponse:&response
error:&error];

if ([data length] >
0 && error == nil){

//
同步返回数据-成功

NSLog(@"%lu bytes of data wasreturned.", (unsigned
long)[data
length]);

}else
if([data length] ==
0 && error == nil){

//
同步返回数据-成功

NSLog(@"No data was returned.");

}else
if(error != nil){

//失败

NSLog(@"Error happened = %@", error);

NSLog(@"We are done.");

}
}

二、NSURLConnection-异步请求网络数据

-(void)viewDidAppear:(BOOL)animated{
[super
viewDidAppear:animated];

NSString
*urlAsString =@"http://www.baidu.com";

NSURL *url = [NSURL
URLWithString:urlAsString];

NSURLRequest *urlRequest =[NSURLRequest
requestWithURL:url

cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData

timeoutInterval:30.0f];

NSOperationQueue
*queue = [[NSOperationQueue alloc]init];

[NSURLConnectionsendAsynchronousRequest:urlRequest

queue:queue

completionHandler:^(NSURLResponse *response,
NSData *data, NSError *error) {

if ([datalength] >0 && error ==
nil){
NSString *html = [[NSString
alloc] initWithData:data
encoding:NSUTF8StringEncoding];
NSLog(@"HTML =
%@", html);
}elseif ([data
length] == 0 && error ==
nil){
NSLog(@"Nothing
was downloaded.");
}elseif (error !=
nil){
NSLog(@"Error
happened = %@", error);
}
}];

NSLog(@"main thread!");

}

三、NSMutableURLRequest-可变网络请求

有时候,我们希望分来来进行配置网络请求,我们可以用NSMutableURLRequest类

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

NSMutableURLRequest *urlRequest =[[NSMutableURLRequest
alloc]init];

[urlRequestsetTimeoutInterval:30.0f];
[urlRequest
setURL:url];

四、GET方式网络请求-NSMutableURLRequest

-(void)viewDidAppear:(BOOL)animated{
[super
viewDidAppear:animated];

NSString
*urlAsString =@"http://www.My_URL_Address.php";

//
为GET方式添加参数
urlAsString = [urlAsStringstringByAppendingString:@"?param1=First"];
urlAsString = [urlAsStringstringByAppendingString:@"¶m2=Second"];

NSURL *url = [NSURL
URLWithString:urlAsString];

NSMutableURLRequest
*urlRequest = [NSMutableURLRequestrequestWithURL:url];
[urlRequest
setTimeoutInterval:30.0f];
[urlRequest
setHTTPMethod:@"GET"];

NSOperationQueue
*queue = [[NSOperationQueue alloc]init];

[NSURLConnectionsendAsynchronousRequest:urlRequest

queue:queue

completionHandler:^(NSURLResponse *response,NSData *data,
NSError *error) {

if ([datalength] >0 && error ==
nil){

NSString *html = [[NSString
alloc] initWithData:data
encoding:NSUTF8StringEncoding];

NSLog(@"HTML =
%@", html);
}elseif ([data
length] == 0 && error ==
nil){

NSLog(@"Nothing
was downloaded.");
}elseif (error !=
nil){
NSLog(@"Error
happened = %@", error);
}

}];

}

五、POST方式网络请求-NSMutableURLRequest

其实很简单,只要设置请求方式为:POST,并且传递请求数据就可以了。

[urlRequest
setHTTPMethod:@"POST"];

NSString
*body=@"bodyParam1=BodyValue1&bodyParam2=BodyValue2";

[urlRequest setHTTPBody:[body dataUsingEncoding:NSUTF8StringEncoding]];
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: