7

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
Community
  • 1
  • 1
PeterD
  • 642
  • 1
  • 6
  • 17
  • The thread you are linking to is about *receiving* chunked data, not about *posting* chunked data. - As I understand it (I may be wrong), the only advantage of the chunked transfer-encoding is that you don't need to know the Content-Size in advance. But if you have the `soapBody` as string or data object then you know the size, so why do you want to use the chunked encoding? - But perhaps I misunderstand something. – Martin R Feb 28 '13 at 20:51
  • Thanks, Martin, yes, I agree, that thread is about receiving. The reason I am interested in sending is that when request gets too large and arrives as a single "raw" SOAP request the server cannot process it. I am not sure why but this seems to be the fact of life - it simply drops it. I am not advanced enough to know why the server behaves as it does. But I do know that when the request is broken into HTTP 1.1 chunks, the server is able to process it just fine - I have the Android code doing that and it works. So, I am trying to do the same. – PeterD Mar 01 '13 at 02:08
  • Another reason to use chunked requests for me is that if the video becomes very large, then instead of loading it all into memory and encoding and sending, I could load it in parts, encode it in parts and send those parts over as chunks. So, I won't have the problem of holding huge objects in the memory. – PeterD Mar 01 '13 at 02:34

1 Answers1

5

Figured it out:

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

This causes the request automatically be in HTP 1.1 chunks!

PeterD
  • 642
  • 1
  • 6
  • 17
  • 4
    Is the size if the chunk specifiable? – Morkrom Nov 06 '14 at 20:58
  • 1
    did your final solution use AFNetworking or your own ChunkedTransferConnection class? can you share? – Anona112 Nov 17 '15 at 20:58
  • @Anona112 I use NSURLConnection, do you know some ways for specify the chunk size. For more details see http://stackoverflow.com/questions/33775734/how-to-specify-http-chunked-size-for-nsurlconnection – Husein Behboudi Rad Nov 18 '15 at 09:10
  • Sorry, I passed this code to another programmer and not sure what the final solution was. It has been a while at this point. – PeterD Nov 19 '15 at 05:39