简体   繁体   English

NSURLConnection:gzip编码的SOAP响应被破坏

[英]NSURLConnection: gzip encoded SOAP response gets corrupted

I'm having trouble writing a IPhone/IOS Obj-C SOAP client that talks to an application with a SOAP WS interface. 我在编写与具有SOAP WS接口的应用程序通信的IPhone / IOS Obj-C SOAP客户端时遇到了麻烦。 The application uses a NuSOAP php webserver and encodes any payload above a certain size using gzip/deflate, whichever is enabled by the client. 该应用程序使用NuSOAP php webserver并使用gzip / deflate对任何超过一定大小的有效负载进行编码,无论客户端启用哪个。

I understand that NSURLConnection transparently decompresses any gzip encoded response and presents the decompressed response, but the raw response that is received in this case seems to be corrupt. 我知道NSURLConnection透明地解压缩任何gzip编码的响应并呈现解压缩的响应,但在这种情况下收到的原始响应似乎是损坏的。 I dumped the SOAP payload into a file and decompressed it using gunzip, it complains of an "unexpected end of file". 我将SOAP有效负载转储到一个文件中,并使用gunzip解压缩它,它抱怨“意外的文件结束”。 I did check the webserver and dumped the gzip response that it is sending to a file, this is decompressed without any error using gunzip. 我确实检查了网络服务器并将其发送到文件的gzip响应转储,使用gunzip解压缩时没有任何错误。 Seems like the response is getting corrupted on receipt. 似乎响应在收到时已损坏。

I tried using both NSURLConnection and ASIHTTPRequest. 我尝试使用NSURLConnection和ASIHTTPRequest。 With NSURLConnection, there is a difference of exactly 15 bytes every time, between the response Data length and the length mentioned in the response's HTTP header. 使用NSURLConnection,响应数据长度和响应的HTTP头中提到的长度之间每次恰好有15个字节的差异。 With ASIHTTPRequest the number of bytes received and the length of the response in the HTTP Header match but alas the response is still corrupt and doesn't respond to gzip decompression kindly. 使用ASIHTTPRequest,接收的字节数和HTTP头中的响应长度匹配,但是响应仍然是损坏的,并且不会响应gzip解压缩。

    - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
    NSLog(@"Headers :%@",[(NSHTTPURLResponse*)response allHeaderFields]);
        [self.receivedData setLength:0];
    self.receivedData = [[NSMutableData dataWithCapacity:1024*1024] retain];
   }

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)value {
    NSString *dataRec = [[NSString alloc] initWithData:value encoding:NSUTF8StringEncoding];
    NSLog(@"didReceiveData :%@",dataRec);
    [self.receivedData appendData:value];
}

2010-10-04 13:37:15.310 SugarSoap[848:207] Headers :{ "Cache-Control" = "no-store, no-cache, must-revalidate, post-check=0, pre-check=0"; 2010-10-04 13:37:15.310 SugarSoap [848:207]标题:{“Cache-Control”=“no-store,no-cache,must-revalidate,post-check = 0,pre-check = 0” ; Connection = "Keep-Alive"; 连接=“保持活力”; "Content-Encoding" = gzip; “Content-Encoding”= gzip; "Content-Length" = 1683; “内容长度”= 1683; "Content-Type" = "text/xml; charset=UTF-8"; “Content-Type”=“text / xml; charset = UTF-8”; Date = "Mon, 04 Oct 2010 08:07:14 GMT"; Date =“Mon,04 Oct 2010 08:07:14 GMT”; Expires = "Thu, 19 Nov 1981 08:52:00 GMT"; Expires =“Thu,1981年11月19日08:52:00 GMT”; "Keep-Alive" = "timeout=15, max=100"; “Keep-Alive”=“timeout = 15,max = 100”; Pragma = "no-cache"; Pragma =“no-cache”; Server = "Apache/2.0.59 (Unix) mod_ssl/2.0.59 OpenSSL/0.9.8g DAV/2 PHP/5.2.5"; Server =“Apache / 2.0.59(Unix)mod_ssl / 2.0.59 OpenSSL / 0.9.8g DAV / 2 PHP / 5.2.5”; "Set-Cookie" = "PHPSESSID=udsgtttvts90ijuhsvuqop6ja6; path=/"; “Set-Cookie”=“PHPSESSID = udsgtttvts90ijuhsvuqop6ja6; path = /”; Vary = "Accept-Encoding"; Vary =“Accept-Encoding”; "X-Powered-By" = "PHP/5.2.5"; “X-Powered-By”=“PHP / 5.2.5”; "X-Soap-Server" = "NuSOAP/0.7.2 ()"; “X-Soap-Server”=“NuSOAP / 0.7.2()”; } 2010-10-04 13:37:15.311 SugarSoap[848:207] 2010-10-04 13:37:15.311 SugarSoap [848:207]

didReceiveData :(null) 2010-10-04 13:37:15.311 SugarSoap[848:207] connectionFinsihed! didReceiveData:(null)2010-10-04 13:37:15.311 SugarSoap [848:207] connectionFinsihed! Length :1668 长度:1668

-(void)requestFinished:(ASIHTTPRequest *)request {
    if([request isResponseCompressed]){
        NSLog(@"Response Compressed.");
    }
    NSData *compressedResponse = [request rawResponseData];
    NSData *responseData = [request responseData];
    NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSASCIIStringEncoding];
    NSLog(@"Length before decompression:%d Length after decompression:%d", compressedResponse length],[responseData length]);
    NSLog(@"Response :%@",responseString);
}

2010-10-04 14:11:20.687 Hello_SOAP[1033:207] Response Compressed. 2010-10-04 14:11:20.687 Hello_SOAP [1033:207]响应压缩。

2010-10-04 14:11:20.687 Hello_SOAP[1033:207] Length before decompression:2165 Length after decompression:0 2010-10-04 14:11:20.687 Hello_SOAP [1033:207]减压前的长度:2165减压后的长度:0

2010-10-04 14:11:20.687 Hello_SOAP[1033:207] Response : 2010-10-04 14:11:20.687 Hello_SOAP [1033:207]回复:

也许这个链接可以帮助你... 代码生成肥皂

I once had a problem when a slightly corrupted file would unzip without any problems on my Mac, but on iPhone it wouldn't. 我曾经有一个问题,当一个稍微损坏的文件在我的Mac上没有任何问题解压缩,但在iPhone上它不会。 I guess zlib on iPhone has less tolerance for errors. 我猜iPhone上的zlib对错误的容忍度较低。

Are you sure that -connection:didReceiveData: isn't being called at least once before -connection:didReceiveResponse: ? 你确定-connection:didReceiveData:-connection:didReceiveResponse:之前至少没有被调用过一次-connection:didReceiveResponse: ? It looks like you might be truncating some of the data that comes in the first packet with the headers. 看起来您可能会截断带有标头的第一个数据包中的一些数据。

Are you sure that your data is UTF8? 您确定您的数据是UTF8吗? Try other encodings and see if you error goes away. 尝试其他编码,看看你的错误是否消失。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM