【发布时间】:2013-07-22 09:41:09
【问题描述】:
我正在 iOS 上开发一个应用程序,请求使用 SOAP 的 Web 服务,但数据类型 xsd:base64Binary (XML) 存在问题。
我根据来自 Web 服务的预期字段构建我的 SOAP 信封。当类型是 xsd:string 或 xsd:integer 或任何其他简单类型时,我没有问题。 但是当我尝试将 xsd:base64Binary 类型添加到 SOAP 信封时,Web 服务无法正确接收数据;这似乎是一个编码问题,但我无法弄清楚。
例如,信封看起来像:
<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema- to instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
<soap12:Body>
<hereTheMethod xmlns="http://heretheaddress/">
<aBase64>/9j/4AAQSkZJRgAB...
...RRQAUUUUAf/Z</aBase64>
<aBool>false</aBool>
<anInt>89</anInt>
<aDouble>0.0</aDouble>
<aString>Hello</aString>
</hereTheMethod>
</soap12:Body>
</soap12:Envelope>
然后网络服务读取“false”、“89”、“0.0”或“Hello”没有任何问题。但是对于 base64,我有类似“����JFIF��XExifMM*�i&...”之类的编码问题。
在 Objective-C 中,我是这样进行的:
NSMutableString *envelopeText = [NSMutableString stringWithFormat:@"(header)\n"
"<aBase64>%@</aBase64>\n"
"(footer)", base64String];
// Some code...
NSData *envelope = [envelopeText dataUsingEncoding:NSUTF8StringEncoding];
// I launch the connection.
最后,当我构建 base64 字符串时,我注意用 UTF-8 对其进行编码:
NSString *base64String = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
请注意,该应用也适用于 Android (Java),并且可以正确使用相同的 Web 服务。
感谢您的帮助!
PS : 对不起我的英语:/
【问题讨论】:
标签: objective-c xml soap base64