7

我有以下问题:我正在创建一个非常大的 SOAP 请求(数据是编码为 Base64 字符串的视频),因此我无法将其作为原始 SOAP 请求发送,而是需要以 HTTP 1.1 块的形式发送。我似乎无法弄清楚该怎么做。我在这里使用了代码: What are alternatives to NSURLConnection for chunked transfer encoding 但它似乎没有做我认为应该做的事情 - 我可以看到请求作为单个请求而不是多个块到达服务器(我在服务器上使用 WireShark 来查看传入的流量。)

我知道 Android 上的类似功能使用 Java 的 Apache Foundations HTTP 库工作 - 有了这些,任何未预先指定长度的 HTTP 请求都作为 HTTP 1.1 块请求传输 - 我确实可以看到这些请求到达服务器作为单独的块......我想效仿。

(更新:在我看来 AFNetworking 可能具有该功能,但我找不到任何关于如何使用它的示例。)

这是我的代码,或多或少:

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];

其中 ChunkedTransferConnection 实现如下

@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
4

1 回答 1

5

弄清楚了:

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

这会导致请求自动位于 HTP 1.1 块中!

于 2013-03-01T16:51:31.103 回答