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
HTTP/1.1 uses content-coding values in the Accept-Encoding (section 14.3) and Content-Encoding (section 14.11) header fields. Although the value describes the content-coding, what is more important is that it indicates what decoding mechanism will be required to remove the encoding.
Chunked transfer encoding is a streaming data transfer mechanism available in version 1.1 of the Hypertext Transfer Protocol (HTTP). In chunked transfer encoding, the data stream is divided into a series of non-overlapping "chunks". The chunks are sent out and received independently of one another.
Figured it out:
NSInputStream *dataStream = [NSInputStream inputStreamWithData:[soapBody dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBodyStream:dataStream];
This causes the request automatically be in HTP 1.1 chunks!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With