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?

terrinecold
  • 231
  • 1
  • 9

2 Answers2

0

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

[lInnerRequest setHTTPMethod:@"POST"];
Saurabh Passolia
  • 8,099
  • 1
  • 26
  • 41
-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

msp
  • 3,272
  • 7
  • 37
  • 49