网络请求

网络请求方式:

HTTP请求方式:

  • GET请求:通常用于请求URL并得到资源
  • POST请求:用于向服务器添加新的内容

网络连接方式:

URL连接方式:

  • 同步连接:UI“冻结”,通常用于请求的数据量小时
  • 异步连接:UI“不冻结”,适应传输数据量大(像图片)



*****************************************************************************


GET同步连接



//新建一个URL 

NSURL *url = [NSURL URLWithString:@"http://api.hudong.com/iphonexml.do?type=focus-c"];

//创建⼀个请求

NSURLRequest * request = [NSURLRequest requestWithURL:url(请求访问路径) cachePolicy:NSURLRequestReloadIgnoringCacheData(缓存协议) timeoutInterval:10.0f(网络请求超时时间)];

//发送同步连接

NSData * data = [NSURLConnection sendSynchronousRequest:request

returningResponse:nil error:nil];

//打印字符串信息

NSString * str = [[NSString alloc]initWithData:data

encoding:NSUTF8StringEncoding]; 

NSLog(@"str = %@",str);





*****************************************************************************


POST同步连接


//新建一个URL 

NSURL *url = [NSURL URLWithString:@"http://api.hudong.com/iphonexml.do?type=focus-c"];

//创建⼀个请求

NSMutableURLRequest * request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:10.0f];

//将请求方式设置为POST(默认为GET)

[request setHTTPMethod:@"POST"];

//发送同步连接

NSData * data = [NSURLConnection sendSynchronousRequest:request

returningResponse:nil error:nil];

//打印字符串信息

NSString * str = [[NSString alloc]initWithData:data

encoding:NSUTF8StringEncoding]; 

NSLog(@"str = %@",str);



*****************************************************************************



GET异步连接



//初始化一个URL

NSURL *url = [NSURL URLWithString:@"http://api.hudong.com/iphonexml.do?type=focus-c"];

//创建一个请求

NSURLRequest * request = [NSURLRequest requestWithURL:url]; 

//发起异步连接

[NSURLConnection

connectionWithRequest:request delegate:self]; 

//其他的事情交给代理⽅方法去完成

NSLog(@"NSURLConnection 完成");






*****************************************************************************



POST异步连接




//创建一个URL

NSURL *url = [NSURL URLWithString:@"http://api.hudong.com/iphonexml.do?type=focus-c"];

//创建一个可变请求

NSMutableURLRequest * request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:10.0f]; 

//设置POST请求⽅方式

[request setHTTPMethod:@"POST"];

//发起异步连接

[NSURLConnection

connectionWithRequest:request delegate:self]; 

//其他的事情交给代理⽅方法去完成

NSLog(@"NSURLConnection 完成");




*****************************************************************************


异步请求代理方法:


//接收到服务器回应的时候调用

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


//接收到服务器传输数据的时候调用

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


//数据传输完之后调用

- (void)connectionDidFinishLoading:(NSURLConnection *)connection


//网络请求过程中,出现任何错误的时候调用

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



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值