如何比较压缩与未压缩的ASIHTTPRequest数据?

时间:2011-12-08 04:17:09

标签: iphone ios gzip asihttprequest

我目前正在从服务器接收一些压缩数据。 我想知道是否有办法检查节省了多少带宽但是检查压缩数据的大小与未压缩数据的对比?

这是我的代码看起来像

//Communication Test
- (IBAction)sendHttpsRequest
{   
    //Start HUD
    [SVProgressHUD showWithMaskType:SVProgressHUDMaskTypeBlack];
    [SVProgressHUD setStatus:@"loading..."];

    //Set request address
    NSMutableString *databaseURL = [[NSMutableString alloc] initWithString:@"https://192.168.17.36:443"];

    //call ASIHTTP delegates (Used to connect to database)
    NSURL *url = [NSURL URLWithString:databaseURL];

    //This sets up all other request
    ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];

    [request setDelegate:self];
    [request setValidatesSecureCertificate:NO];
    [request setPostValue:@"ClientDataSet.xml" forKey:@"filename"];  

    [request startSynchronous];

    BOOL dataWasCompressed = [request isResponseCompressed]; // Was the response gzip compressed?
    NSData *compressedResponse = [request rawResponseData]; // Compressed data
    NSData *uncompressedData = [request responseData]; // Uncompressed data
    NSString *response = [request responseString]; // Uncompressed data as a string

    //Output responses    
    NSLog(@"compressed? %@", dataWasCompressed ? @"YES" : @"NO" );
    NSLog(@"compressed Response = %@", compressedResponse);
    NSLog(@"uncompressed Data = %@", uncompressedData);
    NSLog(@"response = %@", response);
}

- (void)requestFinished:(ASIHTTPRequest *)request
{       
    self.hudCheck = YES;

    responseString = [request responseString]; //Pass requested text from server over to NSString
    NSLog(@"Response String = %@", responseString);

    //removies the SVProgressHUD
    [NSTimer scheduledTimerWithTimeInterval:0.25 target:self selector:@selector(stopAnimating) userInfo:nil repeats:NO];
}

非常感谢任何帮助

1 个答案:

答案 0 :(得分:1)

方法1 :由于ASIHttpRequest使用gzip解压缩数据,因此查看压缩率的一种简单方法是在服务器和放大器上使用gzip ;看看文件有多小。

在正常情况下,即在压缩文本文件时,gzip提供接近80%的压缩率。

方法2:如果您需要通过无线传输确切数量的压缩数据,则需要实施request:didReceiveBytes:request:incrementDownloadSizeBy:。两者都是ASIHttpRequest中的下载跟踪器,主要用于显示下载进度条。