0

我有 xml 字符串。是错误的字符串。但是在使用 NSLog 并从控制台处理它之后,它变成了很好的字符串(我从服务器得到了很好的响应)。我通过这种方式构建了错误的字符串:

NSString *soapMessage = [NSString stringWithFormat:@"<?xml version=\"1.0\" encoding=\"UTF-8\"?><env:Envelope xmlns:env=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:enc=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns:ns0=\"http://com.test/types/AstoriaObjectProperties\" xmlns:ns1=\"http://com.test/wsdl/AstoriaObjectProperties\" xmlns:ns2=\"http://java.sun.com/jax-rpc-ri/internal\"><env:Body><ns1:setObjectProperties env:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\"><HashMap_1 xsi:type=\"ns2:hashMap\" enc:arrayType=\"ns2:mapEntry[2]\"><item xsi:type=\"ns2:mapEntry\"><key xsi:type=\"xsd:string\">id</key><value xsi:type=\"xsd:string\">%@</value></item><item xsi:type=\"ns2:mapEntry\"><key xsi:type=\"xsd:string\">annotations</key><value href=\"#ID1\"/></item></HashMap_1><HashMap_2 xsi:type=\"ns2:hashMap\" enc:arrayType=\"ns2:mapEntry[2]\"><item xsi:type=\"ns2:mapEntry\"><key xsi:type=\"xsd:string\">id</key><value xsi:type=\"xsd:string\">%@</value></item><item xsi:type=\"ns2:mapEntry\"><key xsi:type=\"xsd:string\">annotations</key><value href=\"#ID2\"/></item></HashMap_2></ns1:setObjectProperties>%@%@</env:Body></env:Envelope>", fileID, fileID, arrayNewXML, arrayOldXML];

当我将此消息发送到服务器时,我收到了一个错误响应。

但后来我使用 NSLog 查看soapMessage,我复制它,并使用缓冲区中的soap 消息。它看起来像这样。

<?xml version="1.0" encoding="UTF-8"?><env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:enc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:ns0="http://com.test/types/AstoriaObjectProperties" xmlns:ns1="http://com.test/wsdl/AstoriaObjectProperties" xmlns:ns2="http://java.sun.com/jax-rpc-ri/internal"><env:Body><ns1:setObjectProperties env:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"><HashMap_1 xsi:type="ns2:hashMap" enc:arrayType="ns2:mapEntry[2]"><item xsi:type="ns2:mapEntry"><key xsi:type="xsd:string">id</key><value xsi:type="xsd:string">0000001aWGA2070GYZ</value></item><item xsi:type="ns2:mapEntry"><key xsi:type="xsd:string">annotations</key><value href="#ID1"/></item></HashMap_1><HashMap_2 xsi:type="ns2:hashMap" enc:arrayType="ns2:mapEntry[2]"><item xsi:type="ns2:mapEntry"><key xsi:type="xsd:string">id</key><value xsi:type="xsd:string">0000001aWGA2070GYZ</value></item><item xsi:type="ns2:mapEntry"><key xsi:type="xsd:string">annotations</key><value href="#ID2"/></item></HashMap_2></ns1:setObjectProperties><ns0:ArrayOfhashMap id="ID1" env:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xsi:type="enc:Array" enc:arrayType="ns2:hashMap[1]"><item xsi:type="ns2:hashMap" enc:arrayType="ns2:mapEntry[7]"><item xsi:type="ns2:mapEntry"><key xsi:type="xsd:string">$$$XD_AnnotationUser$$$</key><value xsi:type="xsd:string">AstoriaAdmin</value></item><item xsi:type="ns2:mapEntry"><key xsi:type="xsd:string">Annotation Status</key><value xsi:type="xsd:string">waiting for decision</value></item><item xsi:type="ns2:mapEntry"><key xsi:type="xsd:string">$$$XD_AnnotationID$$$</key><value xsi:type="xsd:string">00000043X951EE3073607F44F7D00000000000000951FF3076D000000590000003C647C7668696D696B2D6163733A433A5C50726F6772616D2046696C65735C4173746F72696120536F6674776172655C434D535C64617461626173655C63616231303030302E64627C31347C367C3263307C31303030303E00704B585000000000YZ</value></item><item xsi:type="ns2:mapEntry"><key xsi:type="xsd:string">Annotation Comment</key><value xsi:type="xsd:string">comment</value></item><item xsi:type="ns2:mapEntry"><key xsi:type="xsd:string">$$$XD_AnnotationDate$$$</key><value xsi:type="enc:dateTime">2012-09-18T10:22:40.000Z</value></item><item xsi:type="ns2:mapEntry"><key xsi:type="xsd:string">Annotation Status Comment</key><value xsi:type="xsd:string">status comment</value></item><item xsi:type="ns2:mapEntry"><key xsi:type="xsd:string">$$$XD_AnnotationName$$$</key><value xsi:type="xsd:string">Legal Review</value></item></item></ns0:ArrayOfhashMap><ns0:ArrayOfhashMap id="ID2" env:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xsi:type="enc:Array" enc:arrayType="ns2:hashMap[1]"><item xsi:type="ns2:hashMap" enc:arrayType="ns2:mapEntry[7]"><item xsi:type="ns2:mapEntry"><key xsi:type="xsd:string">$$$XD_AnnotationUser$$$</key><value xsi:type="xsd:string">AstoriaAdmin</value></item><item xsi:type="ns2:mapEntry"><key xsi:type="xsd:string">Annotation Status</key><value xsi:type="xsd:string">waiting for decision</value></item><item xsi:type="ns2:mapEntry"><key xsi:type="xsd:string">$$$XD_AnnotationID$$$</key><value xsi:type="xsd:string">00000043X951EE3073607F44F7D00000000000000951FF3076D000000590000003C647C7668696D696B2D6163733A433A5C50726F6772616D2046696C65735C4173746F72696120536F6674776172655C434D535C64617461626173655C63616231303030302E64627C31347C367C3263307C31303030303E00704B585000000000YZ</value></item><item xsi:type="ns2:mapEntry"><key xsi:type="xsd:string">Annotation Comment</key><value xsi:type="xsd:string">comment</value></item><item xsi:type="ns2:mapEntry"><key xsi:type="xsd:string">$$$XD_AnnotationDate$$$</key><value xsi:type="enc:dateTime">2012-09-18T10:22:40.000Z</value></item><item xsi:type="ns2:mapEntry"><key xsi:type="xsd:string">Annotation Status Comment</key><value xsi:type="xsd:string">status comment</value></item><item xsi:type="ns2:mapEntry"><key xsi:type="xsd:string">$$$XD_AnnotationName$$$</key><value xsi:type="xsd:string">Legal Review</value></item></item></ns0:ArrayOfhashMap></env:Body></env:Envelope>

之后我得到了没有错误的响应。

我似乎认为 NSLog 解决了一些编码问题。但是如何在我的soapMessage中解决这个问题?我试过:

soapMessage = [soapMessage stringByReplacingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
soapMessage = [soapMessage stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
soapMessage = [soapMessage stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

那没有帮助。

4

1 回答 1

0

我只是客串编码,并手动删除了一些坏符号:

 NSMutableData *soapdata = [[[NSMutableData alloc] initWithData:[soapMessage dataUsingEncoding:NSNonLossyASCIIStringEncoding]] autorelease]; 
   soapMessage = [[[NSString alloc] initWithData:soapdata encoding:NSUTF8StringEncoding] autorelease];
   soapMessage = [soapMessage stringByReplacingOccurrencesOfString:@"\\ufffc" withString:@""];
   soapdata = [[[NSMutableData alloc] initWithData:[soapMessage dataUsingEncoding:NSUTF8StringEncoding]] autorelease];

现在它起作用了!

于 2012-09-20T11:09:03.377 回答