NSURLProtocol Progress and Post-Download

I use the NSURLProtocol subclass to intercept all HTTP calls and change the user agent, as well as add other HTTP headers that my server needs.

-(id)initWithRequest:(NSURLRequest *)request cachedResponse:(NSCachedURLResponse *)cachedResponse client:(id <NSURLProtocolClient>)client { NSMutableURLRequest* lInnerRequest; //************************************************ lInnerRequest = [request mutableCopy]; [lInnerRequest setValue:@"MyUserAgent" forHTTPHeaderField:@"User-Agent"]; //************************************************ self = [super initWithRequest:lInnerRequest cachedResponse:cachedResponse client:client]; //************************************************ if (self) { self.innerRequest = lInnerRequest; } //***********************************00************* [lInnerRequest release]; //************************************************ return self; } 

My protocol uses NSURLConnection

 - (void)startLoading { self.URLConnection = [NSURLConnection connectionWithRequest:self.innerRequest delegate:self]; } 

Then I implement the entire delegate method in NSURLConnection, forwarding the call to the equivalent NSURLProtocolClient method. This works well, but when I upload data to the server, my code that uses NSURLConnection does not return:

connection: didSendBodyData: totalBytesWritten: totalBytesExpectedToWrite:

I understand why this has been happening since I did not implement this method in NSURLProtocol, since there is no equivalent NSURLProtocolClient method that can be used to report progress.

Has anyone found any workaround for this?

+6
source share
2 answers

add this line to your code to make an HTTP POST request:

 [lInnerRequest setHTTPMethod:@"POST"]; 
0
source

Use setProperty:forKey:inRequest: to save the NSURLConnection and delegate the object, then use propertyForKey:inRequest: to load the NSURLConnection object and delegate the object to the custom NSURLProtocol class, when sending data, use the NSURLConnection object and delegate its delegate method call

-1
source

Source: https://habr.com/ru/post/907860/


All Articles