1

我需要通过 SOAP 请求与服务器通信并获得响应。我有一个很好的方法,它使用参数创建 XML 请求,将其转换为 NSMutableURLRequest 并使用 NSURLConnection 发送。所有这些都很好,所以我将跳过这部分代码。我的服务器有点像 Magento 商店,所以我收到不同数量的数据取决于我使用的请求。当我收到会话 ID 之类的简短回复时,一切正常,但当回复较长时(例如国家列表),我的数据会以某种方式丢失。我通过比较 didReceiveResponse 和 didReceiveData 中的数据长度来检查它

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    self.expectedLength = response.expectedContentLength;
    self.downloadedLength = 0;
    [receivedData setLength:0];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    NSLog(@"Data recived");
    self.downloadedLength = self.downloadedLength + [data length];
    [receivedData appendData:data];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
        if (self.downloadedLength == self.expectedLength) {
            NSLog(@"correctly downloaded");
        }
        else{
            NSLog(@"sizes don't match");
        }

        str = [[NSString alloc] initWithBytes:[receivedData bytes] length:[receivedData length] encoding:NSISOLatin1StringEncoding];

        NSData *tmp_Data = [[NSData alloc] initWithData:[str dataUsingEncoding:NSISOLatin1StringEncoding allowLossyConversion:YES]];

        parser = [[SYXmlParser alloc]initWithData:tmp_Data];

        [parser startParser];

        if([parser theDataArray] != nil && [[parser theDataArray]count] != 0)
        {
            resultArray = [[NSMutableArray alloc]initWithArray:[parser theDataArray]];
            [self performSelectorOnMainThread:@selector(loadFinished) withObject:nil waitUntilDone:YES];
        }

    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
}

当响应很长时,我得到了剪切 XML,并且我的比较返回了“大小不匹配”。XML 不是绝对减半,而是文本中间缺少数据,就像只缺少一些字符一样。这个问题每次都以相同的方式在相同的地方重复,所以它不是随机的。我尝试使用 AFNetworking 来解决这个问题,但我认为我无法通过这个 SOAP 请求正确使用它。我会感谢每一个能解决这个问题的提议。

编辑:

我使用了这个 Charles,但存在与 xcode 相同的问题。当我打开响应选项卡和 SOAP 选项卡时,响应为空,在 XML 选项卡上出现 Charles 无法解析接收到的数据的错误,但在概览选项卡中,响应的大小为 4666 字节。这是否意味着服务器给出了不好的响应?我不敢相信它,因为它的商业服务器 Magento 与许多其他语言一起使用并且可以正常工作。

4

0 回答 0