0

我有 SOAP 网络服务,我尝试连接它。但 SOAP 响应包含“System.Xml.XmlException:缺少根元素”。

我的 obj-c 代码。怎么了?

NSString *soapMessage = [NSString stringWithFormat:
                         @"<?xml version=\"1.0\" encoding=\"utf-8\"?>"
                         "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">"
                         "<soap:Body>"
                         "<Method1 xmlns=\"http://tempuri.org/\">"
                         "<param1>string</param1>"
                         "</Method1>"
                         "</soap:Body>"
                         "</soap:Envelope>"];

NSLog(soapMessage);

NSURL *url = [NSURL URLWithString:@"web service url"];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
NSString *msgLength = [NSString stringWithFormat:@"%d", [soapMessage length]];


[theRequest addValue: @"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[theRequest addValue: msgLength forHTTPHeaderField:@"Content-Length"];
[theRequest addValue: @"\"http://tempuri.org/Method1\"" forHTTPHeaderField:@"SOAPAction"];
[theRequest setHTTPMethod:@"POST"];
[theRequest setHTTPBody: [soapMessage dataUsingEncoding:NSUTF8StringEncoding]];

NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];

if( theConnection )
{
    webData = [[NSMutableData data] retain];
}
else
{
    NSLog(@"theConnection is NULL");
}
4

2 回答 2

1
NSString *soapMessage = [NSString stringWithFormat:
                         @"<?xml version=\"1.0\" encoding=\"utf-8\"?>"
                         "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">"
                         "<soap:Body>"
                         "<Method1 xmlns=\"http://tempuri.org/\">"
                         "<param1>%@</param1>"
                         "</Method1>"
                         "</soap:Body>"
                         "</soap:Envelope>",str];
Str here is a string that you need to pass for param1.
Hope This will work for you.
于 2012-10-23T22:07:43.280 回答
1

您需要从以下位置删除额外的引号:

[theRequest addValue: @"\"http://tempuri.org/Method1\"" forHTTPHeaderField:@"SOAPAction"];

它应该是:

[theRequest addValue: @"http://tempuri.org/Method1" forHTTPHeaderField:@"SOAPAction"];

希望有帮助。

于 2012-10-23T22:45:14.700 回答