微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用wcf框架进行Web服务时,objective-c-xml解析错误

这是我第一次在iOS应用程序上使用wcf soap服务时,在尝试使用post方法将xml发送到服务器时遇到问题我的iOS端代码看起来像那样
Nsstring *soapMessage = [Nsstring stringWithFormat:@"<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\"><SOAP-ENV:Body><SaveAllRecords33><xmlData></xmlData></SaveAllRecords33></SOAP-ENV:Body></SOAP-ENV:Envelope>"];

NSURL *url = [NSURL URLWithString:@"http://webservice.net2survey.com/servicejson.svc/SaveAllRecord33"];
NSMutableuRLRequest *theRequest = [NSMutableuRLRequest requestWithURL:url];                        
Nsstring *msgLength = [Nsstring stringWithFormat:@"%d",[soapMessage length]];             

[theRequest addValue: @"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[theRequest addValue: @"urn:ServiceJSON/SaveAllRecords33" forHTTPHeaderField:@"SOAPAction"];
[theRequest addValue: msgLength forHTTPHeaderField:@"Content-Length"];
[theRequest setHTTPMethod:@"POST"];    
[theRequest setHTTPBody: [soapMessage dataUsingEncoding:NSUTF8StringEncoding]];

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

在.net方面它就是这样的

[WebInvoke(Method = "POST",BodyStyle = WebMessageBodyStyle.WrappedRequest,ResponseFormat = Webmessageformat.Json)] 
[OperationContract] 
public string SaveAllRecords33(string xmlData) 
{ 
    //DataContractSerializer
    dataContract = new DataContractSerializer(typeof(string)); 
    XmlDocument xmlDoc = new XmlDocument(); 
    xmlDoc.LoadXml(xmlData);

    JavaScriptSerializer objSerialiver = new JavaScriptSerializer();
    return "{\"Answer\":" + objSerialiver.Serialize(xmlDoc.InnerXml) + "}"; 
}

当目标要打到这个网址时,它显示出一些错误,如下所示

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"><s:Body><s:Fault><faultcode xmlns:a="http://schemas.microsoft.com/net/2005/12/windowscommunicationfoundation/dispatcher">a:InternalServiceFault</faultcode><faultstring xml:lang="en-US">Root element is missing.</faultstring><detail><ExceptionDetail xmlns="http://schemas.datacontract.org/2004/07/System.ServiceModel" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"><HelpLink i:nil="true"/><InnerException i:nil="true"/><Message>Root element is missing.</Message><StackTrace>   at System.Xml.XmlTextReaderImpl.ThrowWithoutLineInfo(String res)&#xD;
   at System.Xml.XmlTextReaderImpl.ParseDocumentContent()&#xD;
   at System.Xml.XmlLoader.Load(XmlDocument doc,XmlReader reader,Boolean preserveWhitespace)&#xD;
   at System.Xml.XmlDocument.Load(XmlReader reader)&#xD;
   at System.Xml.XmlDocument.LoadXml(String xml)&#xD;
   at WcfService.ServiceJSON.SaveAllRecords33(String xmlData)&#xD;
   at SyncInvokeSaveAllRecords33(Object,Object[],Object[] )&#xD;
   at System.ServiceModel.dispatcher.SyncmethodInvoker.Invoke(Object instance,Object[] inputs,Object[]&amp; outputs)&#xD;
   at System.S
2012-09-25 17:50:00.846 Net2Survey_demo[1576:b903] str: erviceModel.dispatcher.dispatchOperationRuntime.InvokeBegin(MessageRpc&amp; rpc)&#xD;
   at System.ServiceModel.dispatcher.ImmutabledispatchRuntime.ProcessMessage5(MessageRpc&amp; rpc)&#xD;
   at System.ServiceModel.dispatcher.ImmutabledispatchRuntime.ProcessMessage31(MessageRpc&amp; rpc)&#xD;
   at System.ServiceModel.dispatcher.MessageRpc.Process(Boolean isOperationContextSet)</StackTrace><Type>System.Xml.XmlException</Type></ExceptionDetail></detail></s:Fault></s:Body></s:Envelope>

解决方法

服务本身有效,问题不在WCF中.如果您在堆栈跟踪中向右滚动,您将看到它表示缺少Root元素.并且在服务中创建XMLDocument时失败.

您只是发送一个无效的Xml字符串,它无法解析.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐