iphone断点下载,断点续传

(void)loadView {

 NSURLConnection *hc=nil;

@try{ NSURL *url=[NSURL URLWithString:@http://218.206.70.212/SingleDemo/file/480/soumoneyhaidaotuan_1_10001000.dmh];

 

NSMutableURLRequest* request=[NSMutableURLRequest requestWithURL:url];

 [request setValue:@"bytes=200 - 1200" forHTTPHeaderField:@"Range"];

//清空缓存(如果不加这一行,重复调用的时候总显示第一次的数据)

[request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];

//这个是同步网络的调用方法

NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];

//nsdata转换成char  打印读出来的数据

char* bu=[returnData bytes];

for (int i=0; i<[returnData  length];i++){

NSLog(@"打印数据:%d",bu[i]);

 

//异步调用方法

//hc=[NSURLConnection sendSynchronousRequest:request returningResponse:nil err:nil];

//[hc start];

if (hc) {

网络连接好了

}

}

以下方法全是异步时所调用的方法


- (void)connection:(NSURLConnection*)connection didReceiveResponse:
(NSHTTPURLResponse*)response {
//response saved so that status Codes can be checked later
    _response = [response retain];
    NSDictionary* headers = [response allHeaderFields];
    int contentLength = [[headers objectForKey:@"Content-Length"] intValue];
   
    //append the responseData used in connectionDidFinishLoading:
    _responseData = [[NSMutableData alloc] initWithCapacity:contentLength];

}

//获取数据
- (void)connection:(NSURLConnection*)connection didReceiveData:(NSData*)data {
[_responseData appendData:data];
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *error {NSLog(@"Connection failed: %@", [error description]);}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {

[hc release];

}

参考:http://sdlqhjk.iteye.com/blog/964458
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值