NSURLProtocol and post upload progress
Asked Answered
F

2

6

I am using a subclass of NSURLProtocol to intercept all HTTP calls and modify the user agent as well as add a other http headers required by my server.

-(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 then uses an NSURLConnection

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

I then implement all the delegate method in the NSURLConnection by forwarding the call to the equivalent NSURLProtocolClient method. This works well in general but when I am uploading data to the server, my code which is using NSURLConnection does not get called back on:

connection:didSendBodyData:totalBytesWritten:totalBytesExpectedToWrite:

I understand why this is since I didn't implement that method in the NSURLProtocol as there are no equivalent NSURLProtocolClient method which can be used to report upload progress.

Has someone found any workaround for this?

Frugal answered 7/2, 2012 at 16:17 Comment(2)
this is a very interesting problem. did you ever find out a way to do this?Lira
Having the same problem with the exact same use case. Does anyone know any workarounds?Milicent
B
0

add this line to your code to make it a HTTP POST request:

[lInnerRequest setHTTPMethod:@"POST"];
Bendigo answered 7/2, 2012 at 16:22 Comment(1)
This does not work. in any case lInnerRequest = [request mutableCopy]; should have copied the methodFrugal
M
-1

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

Manymanya answered 5/4, 2016 at 11:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.