iphone - "Root element is missing"iOS 中的 Soap 请求

标签 iphone objective-c ios web-services soap

我有 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");
}

最佳答案

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.

关于iphone - "Root element is missing"iOS 中的 Soap 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13039514/

相关文章:

ios - 如何在用户键入时创建增加框架大小而不是增加滚动大小的 TextView

ios - 从用户输入的属性字符串中查找属性

ios - iOS 11 导航栏和屏幕尺寸的布局变化

iphone - 通过 Cocoa Touch 更改 iPhone 应用程序图标

ios - 将curl命令翻译成NSMutableUrlRequest

ios - 在 UIWebView - iOS 中通过 Javascript 访问 UDID

ios - 关闭图像选择器导致崩溃

ios - replaceObjectAtIndex objectforKey 崩溃

ios - 控制 iPad 电源状态的应用程序

iphone - Xcode中抑制 "Undefined symbol"(4)