0

I am using a GET for getting a data from Server, Want to implement a progressbar, for that i tried to capture expectedContentLength in the didReceiveResponse delegate method. How can it can be solved?

NSString* serverUrl = @"https:testURL";

receivedData = [[NSMutableData alloc] initWithLength:0];

NSURL* url =[NSURL URLWithString:[serverUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];

[theRequest setValue:@"identity" forHTTPHeaderField:@"Accept-Encoding"];**(I tried it after seeing other solutions over Web)**

[NSURLConnection connectionWithRequest:theRequest delegate:self];

...

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

    [receivedData setLength:0];
    expectedBytes = [response expectedContentLength];
}
Sunil Prajapati
  • 473
  • 5
  • 17
kiri
  • 1,977
  • 5
  • 27
  • 55
  • 1
    Not all servers (or more precisely, not all requests) return a `Content-Length` header. It is quite frequent for scripts to omit it. If the server is under your control, make sure it sends an appropriate `Content-Length` header. – jcaron Mar 21 '17 at 07:52
  • We are not getting Content-Length in the allHeaders – kiri Mar 21 '17 at 08:40
  • Some [have reported](http://stackoverflow.com/a/17444913/1271826) that `Accept-encoding` of `gzip;q=0` works. Others [have suggested](http://stackoverflow.com/a/11204338/1271826) a zero length string. I've used `identity` listed in your question successfully. – Rob Mar 21 '17 at 09:02

1 Answers1

0
- (void) connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
     _contentLength = [response expectedContentLength];

}
-(void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
 _receiveLength += data.length;
}

Do you think so?

kangbing
  • 16
  • 1