Objective c 对web服务使用wcf框架时发生xml分析错误
这是iam第一次为iOS应用程序处理WCFSOAP服务时,em在尝试使用post方法将xml发送到服务器时遇到问题。我的iOS端代码如下所示Objective c 对web服务使用wcf框架时发生xml分析错误,objective-c,ios,xml,wcf,Objective C,Ios,Xml,Wcf,这是iam第一次为iOS应用程序处理WCFSOAP服务时,em在尝试使用post方法将xml发送到服务器时遇到问题。我的iOS端代码如下所示 NSString *soapMessage = [NSString stringWithFormat:@"<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\"><SOAP-ENV:Body><SaveAllRecords3
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];
当aim点击这个url时,它会显示一些错误,如下所示
<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)
at System.Xml.XmlTextReaderImpl.ParseDocumentContent()
at System.Xml.XmlLoader.Load(XmlDocument doc, XmlReader reader, Boolean preserveWhitespace)
at System.Xml.XmlDocument.Load(XmlReader reader)
at System.Xml.XmlDocument.LoadXml(String xml)
at WcfService.ServiceJSON.SaveAllRecords33(String xmlData)
at SyncInvokeSaveAllRecords33(Object , Object[] , Object[] )
at System.ServiceModel.Dispatcher.SyncMethodInvoker.Invoke(Object instance, Object[] inputs, Object[]& outputs)
at System.S
2012-09-25 17:50:00.846 Net2Survey_demo[1576:b903] str: erviceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin(MessageRpc& rpc)
at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage5(MessageRpc& rpc)
at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage31(MessageRpc& rpc)
at System.ServiceModel.Dispatcher.MessageRpc.Process(Boolean isOperationContextSet)</StackTrace><Type>System.Xml.XmlException</Type></ExceptionDetail></detail></s:Fault></s:Body></s:Envelope>
a:InternalServiceFaultRoot元素缺失。根元素缺失。在System.Xml.XmlTextReaderImpl.ThrowWithoutLineInfo(字符串res)和xD;
在System.Xml.XmlTextReaderImpl.ParseDocumentContent()和#xD;
在System.Xml.xmloader.Load(XmlDocument doc、XmlReader reader、Boolean preserve whitespace)和#xD;
加载(XmlReader)和xD;
在System.Xml.XmlDocument.LoadXml(字符串Xml)和#xD;
在WcfService.ServiceJSON.SaveAllRecords33(字符串xmlData)和xD;
在SyncInvokeSavallRecords33(对象,对象[],对象[])和#xD;
在System.ServiceModel.Dispatcher.SyncMethodInvoker.Invoke(对象实例、对象[]输入、对象[]输出)和#xD;
在System.S
2012-09-25 17:50:00.846 Net2Survey_demo[1576:b903]str:servicecmodel.Dispatcher.dispatcheoperationruntime.InvokeBegin(MessageRpc&;rpc)和xD;
在System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage5(MessageRpc&;rpc)和#xD;
在System.ServiceModel.Dispatcher.ImmutableDispatcheRuntime.ProcessMessage31(MessageRpc&;rpc)和#xD;
位于System.ServiceModel.Dispatcher.MessageRpc.Process(Boolean isOperationContextSet)System.Xml.XmlException
我像这样调用了这种类型的WCF Web服务尝试调用可能对您有所帮助..)
-(void)视图将出现
{
NSString*soapMsg=[NSString stringWithFormat:@]
"%@"
"%@"
"%@"
""
“”,txtToPostalCode.text,txtFrmPostalCode.text,txtWeight.text];
NSURL*url=[NSURL URLWithString:@http://webservice.net2survey.com/servicejson.svc/basic"];
NSMutableURLRequest*req=[NSMutableUrlRequestWithURL:url];
NSString*msgLength=[NSString stringWithFormat:@“%d”,[soapMsg长度]];
[req addValue:@“应用程序/xml;字符集=utf-8”用于HttpHeaderField:@“内容类型”];
[请求添加值:@”http://webservice.net2survey.com/servicejson.svc/basicforHTTPHeaderField:@“SOAPAction”];
[req addValue:msgLength for HttpHeaderField:@“内容长度”];
[req setHTTPMethod:@“POST”];
[req setHTTPBody:[soapMsg dataUsingEncoding:NSUTF8StringEncoding]];
conn=[[NSURLConnection alloc]initWithRequest:req委托:self];
如果(康涅狄格州){
webData=[[NSMutableData]保留];
}
objAppDelegate.arrglbpostatesing=[[NSMutableArray alloc]init];
}
服务本身工作正常,问题不在WCF中。如果在堆栈跟踪中向右滚动,您将看到它显示缺少根元素。
并且在服务中创建XmlDocument时失败
您只是发送了一个无效的Xml字符串,它无法解析。Uhh,当然。valuevalue
<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)
at System.Xml.XmlTextReaderImpl.ParseDocumentContent()
at System.Xml.XmlLoader.Load(XmlDocument doc, XmlReader reader, Boolean preserveWhitespace)
at System.Xml.XmlDocument.Load(XmlReader reader)
at System.Xml.XmlDocument.LoadXml(String xml)
at WcfService.ServiceJSON.SaveAllRecords33(String xmlData)
at SyncInvokeSaveAllRecords33(Object , Object[] , Object[] )
at System.ServiceModel.Dispatcher.SyncMethodInvoker.Invoke(Object instance, Object[] inputs, Object[]& outputs)
at System.S
2012-09-25 17:50:00.846 Net2Survey_demo[1576:b903] str: erviceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin(MessageRpc& rpc)
at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage5(MessageRpc& rpc)
at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage31(MessageRpc& rpc)
at System.ServiceModel.Dispatcher.MessageRpc.Process(Boolean isOperationContextSet)</StackTrace><Type>System.Xml.XmlException</Type></ExceptionDetail></detail></s:Fault></s:Body></s:Envelope>
-(void)ViewWillAppear
{
NSString *soapMsg = [NSString stringWithFormat:@"<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope\">"
"<ToPostalCode>%@</ToPostalCode>"
"<FromPostalCode>%@</FromPostalCode>"
"<Weight>%@</Weight>"
"<DeliveryServiceName></DeliveryServiceName>"
"</SingaporePostalInfoDetailsRequest>",txtToPostalCode.text,txtFrmPostalCode.text,txtWeight.text];
NSURL *url = [NSURL URLWithString: @http://webservice.net2survey.com/servicejson.svc/basic"];
NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url];
NSString *msgLength = [NSString stringWithFormat:@"%d", [soapMsg length]];
[req addValue:@"application/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[req addValue:@"http://webservice.net2survey.com/servicejson.svc/basic" forHTTPHeaderField:@"SOAPAction"];
[req addValue:msgLength forHTTPHeaderField:@"Content-Length"];
[req setHTTPMethod:@"POST"];
[req setHTTPBody: [soapMsg dataUsingEncoding:NSUTF8StringEncoding]];
conn = [[NSURLConnection alloc] initWithRequest:req delegate:self];
if (conn) {
webData = [[NSMutableData data] retain];
}
objAppDelegate.arrGlbPostageSing = [[NSMutableArray alloc]init];
}