iosobjective-csoapnsurlconnectionhttp-chunked

Sending chunked HTTP 1.1 requests in Objective-C


I have the following issue: I am creating a very big SOAP request (the data is a video encoded as Base64 string) and because of that I cannot send it as a raw SOAP request but rather need to send it in HTTP 1.1 chunks. I cannot seem to figure out how to do it. I used the code in here: What are alternatives to NSURLConnection for chunked transfer encoding but it doesn't seem to be doing what I think it should - I can see that the request arrives on the server as a single request instead of many chunks (I am using WireShark on the server to see the incoming traffic.)

I know that a similar functionality on an Android works using Apache Foundations HTTP libraries for Java - with these, any HTTP request whose length is not specified in advance is transmitted as an HTTP 1.1 Chunked Request - and I can see indeed those requests arriving on the server as individual chunks... I want to emulate that.

(UPDATE: Seems to me AFNetworking might have the functionality, but I fail to find any example as to how to use it.)

Here is my code, more or less:

NSString *soapBody = ....; //some correctly formed SOAP request XML here 


NSURL *url = [NSURL URLWithString:...];
NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:url];
[request addValue: ... forHTTPHeaderField:@"SOAPAction"];
[request setHTTPMethod:@"POST"];
[request addValue:@"text/xml" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:[soapBody dataUsingEncoding:NSUTF8StringEncoding]];
ChunkedTransferConnection* connection = [ChunkedTransferConnection alloc];
[connection establishConnectionWithRequest:request];

where ChunkedTransferConnection implementation is the following

@implementation ChunkedTransferConnection

    @synthesize p_connection;
    @synthesize p_responseData;

    - (void)establishConnectionWithRequest:(NSMutableURLRequest *)request
    {
        self.p_responseData = [[NSMutableData alloc] initWithLength:0] ;
        self.p_connection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES];
    }
...
@end

Solution

  • Figured it out:

        NSInputStream *dataStream = [NSInputStream inputStreamWithData:[soapBody dataUsingEncoding:NSUTF8StringEncoding]];
    [request setHTTPBodyStream:dataStream];
    

    This causes the request automatically be in HTP 1.1 chunks!