我有SOAP webservice,我尝试连接它。但SOAP响应包含“System.Xml.XmlException:Root元素缺失”。
我的对象代码。怎么了?
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");
}
答案 0 :(得分: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.
答案 1 :(得分:1)
您需要删除以下内容中的额外报价:
[theRequest addValue: @"\"http://tempuri.org/Method1\"" forHTTPHeaderField:@"SOAPAction"];
应该是:
[theRequest addValue: @"http://tempuri.org/Method1" forHTTPHeaderField:@"SOAPAction"];
希望有所帮助。