2012-09-25 61 views
0

這是第一次,而在WCF SOAP服務IAM工作的iOS應用,EM嘗試使用POST方法我的iOS端代碼看起來像XML而使用WCF框架的Web服務解析錯誤

發送XML到服務器時,得到的問題
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> 
一些錯誤

回答

0

本身作品的服務,這個問題是不是在WCF。如果您在堆棧跟蹤中向右滾動,則會看到它說Root element is missing.,並且在您的服務中創建XmlDocument時失敗。

你只是簡單地給它發送一個無效的Xml字符串,它解析失敗。

+0

好的,你能給我一些有效的XML示例嗎? – Aadil

+0

呃,當然。 ' value value' – Artless

1

我稱這種類型的WCF web服務的這樣的嘗試調用它可能導致您的幫助..)

-(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]; 


}