URLRequest 的一个实例

URLRequest 的一个实例

[html] view plain copy
  1. //Createtherequest.
  2. //所构建的NSURLRequest具有一个依赖于缓存响应的特定策略,cachePolicy取得策略,timeoutInterval取得超时值
  3. NSURLRequest*theRequest=[NSURLRequestrequestWithURL:[NSURLURLWithString:@"http://www.apple.com/"]
  4. cachePolicy:NSURLRequestUseProtocolCachePolicy
  5. timeoutInterval:60.0];
  6. //createtheconnectionwiththerequest
  7. //andstartloadingthedata
  8. NSURLConnection*theConnection=[[NSURLConnectionalloc]initWithRequest:theRequestdelegate:self];
  9. if(theConnection){
  10. //CreatetheNSMutableDatatoholdthereceiveddata.
  11. //receivedDataisaninstancevariabledeclaredelsewhere.
  12. receivedData=[[NSMutableDatadata]retain];
  13. }else{
  14. //Informtheuserthattheconnectionfailed.
  15. }
from:http://blog.csdn.net/bl1988530/article/details/6590099

其中:
NSURLRequest默认的cache policy是 NSURLRequestUseProtocolCachePolicy, 是最能保持一致性的协议。
NSURLRequestReloadIgnoringCacheData忽略缓存直接从原始地址下载
NSURLRequestReturnCacheDataElseLoad只有在cache中不存在data时才从原始地址下载
NSURLRequestReturnCacheDataDontLoad允许app确定是否要返回cache数据,如果使用这种协议当本地不存在response的时候,创建NSURLConnection or NSURLDownload实例时将会马上返回nil;这类似于离线模式,没有建立网络连接;

你只需要实现以下delegate方法来处理数据响应

- (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

NSURLConnect还提供了一个方便的类方法(class method) : sendSynchronousRequest:returningResponse:error:可用来同步地加载一个URL请求

+ (NSData *)sendSynchronousRequest: (NSURLRequest *)request returningResponse: (NSURLResponse **)response error: (NSError **)error

  • request 要装载的URL请求. 这个request 对象 作为初始化进程的一部分,被深度复制(deep-copied). 在这个方法返回之后, 再修改request, 将不会影响用在装载的过程中的request
  • reponse 输出参数, 由服务器返回的URL响应
  • error 输出参数, 如果在处理请求的过程中发生错误,就会使用. 无错误,就为NULL


一个实现异步get请求的例子:
  1. NSString*url=[NSStringstringWithFormat:@"http://localhost/chat/messages.php?past=%ld&t=%ld",
  2. lastId,time(0)];
  3. NSMutableURLRequest*request=[[[NSMutableURLRequestalloc]init]autorelease];
  4. [requestsetURL:[NSURLURLWithString:url]];
  5. [requestsetHTTPMethod:@"GET"];
  6. NSURLConnection*conn=[[NSURLConnectionalloc]initWithRequest:requestdelegate:self];
  7. if(conn)
  8. {
  9. receivedData=[[NSMutableDatadata]retain];
  10. }
  11. else
  12. {
  13. }
  14. -(void)timerCallback{
  15. //[timerrelease];
  16. [selfgetNewMessages];
  17. }
  18. -(void)connection:(NSURLConnection*)connectiondidReceiveResponse:(NSURLResponse*)response
  19. {
  20. [receivedDatasetLength:0];
  21. }
  22. -(void)connection:(NSURLConnection*)connectiondidReceiveData:(NSData*)data
  23. {
  24. [receivedDataappendData:data];
  25. }
  26. -(void)connectionDidFinishLoading:(NSURLConnection*)connection
  27. {
  28. if(chatParser)
  29. [chatParserrelease];
  30. if(messages==nil)
  31. messages=[[NSMutableArrayalloc]init];
  32. chatParser=[[NSXMLParseralloc]initWithData:receivedData];
  33. [chatParsersetDelegate:self];//setthedelegate
  34. [chatParserparse];//startparse
  35. [receivedDatarelease];
  36. [messageListreloadData];
  37. NSInvocation*invocation=[NSInvocationinvocationWithMethodSignature:
  38. [selfmethodSignatureForSelector:@selector(timerCallback)]];
  39. [invocationsetTarget:self];
  40. [invocationsetSelector:@selector(timerCallback)];
  41. //timer=[NSTimerscheduledTimerWithTimeInterval:5.0invocation:invocationrepeats:NO];
  42. [NSTimerscheduledTimerWithTimeInterval:5.0invocation:invocationrepeats:NO];//ifsetyes,thenvery5secondsupdatathetable
  43. }



一个实现同步Get请求的例子:
[html] view plain copy
  1. //初始化请求
  2. NSMutableURLRequest*request=[[NSMutableURLRequestalloc]init];
  3. //设置URL
  4. [requestsetURL:[NSURLURLWithString:urlStr]];
  5. //设置HTTP方法
  6. [requestsetHTTPMethod:@"GET"];
  7. //发送同步请求,这里得returnData就是返回得数据了
  8. NSData*returnData=[NSURLConnectionsendSynchronousRequest:request
  9. returningResponse:nilerror:nil];
  10. //释放对象
  11. [requestrelease];
from:http://blog.csdn.net/bl1988530/article/details/6590099
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值