title: How to check data integrity when using NSURLConnection sendSynchronousRequest?
body: <p>I use sendSynchronousRequest:returningResponse:error method of NSURLConnection class to get NSData from network.</p>
  
  <p><a href="http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSURLConnection_Class/Reference/Reference.html" rel="nofollow">http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSURLConnection_Class/Reference/Reference.html</a></p>
  
  <pre><code>NSData *urlData = [NSURLConnection sendSynchronousRequest:request returningResponse:&amp;response error:&amp;error];
  </code></pre>
  
  <p>What I want to do is to check if returned value is valid or not.
  So, what I did was to compare the length of the data with expected length in response header as below.</p>
  
  <pre><code>NSData *urlData;
  do {
      urlData = [NSURLConnection sendSynchronousRequest:request returningResponse:&amp;response error:&amp;error];
      if ([urlData length] != [response expectedContentLength]) {
          NSLog(@"WTF!!!!!!!!! NSURLConnection response[%@] length[%lld] [%d]", [[response URL] absoluteString], [response expectedContentLength], [urlData length]);
          NSHTTPURLResponse *httpresponse = (NSHTTPURLResponse *) response;
          NSDictionary *dic = [httpresponse allHeaderFields];
          NSLog(@"[%@]", [dic description]);
      }
  } while ([urlData length] != [response expectedContentLength]);
  </code></pre>
  
  <p>However, I don't know if it is enough to ensure the integrity of returned data.
  I can't check checksum of the file on the remote server.</p>
  
  <p>Can you share your experience or other tips?</p>
  
  <p>Thanks.</p>