2013-11-04 42 views
0

我發送SOAP請求。但沒有數據正在返回。該Web服務不會返回任何錯誤。我無法弄清楚什麼是錯的。它與UTF8編碼有關嗎?請幫忙。下面是我的代碼:皁響應空IOS

NSString *xmlString = @"<ServiceCalls xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"><ServiceCall><Phone_No>033542390843</Phone_No></ServiceCall></ServiceCalls>"; 

       NSString *soapMessage = [NSString stringWithFormat:@"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" 
         "<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/\">\n" 
         "<soap:Body>\n" 
         "<GetVehiclesByPhone_ServiceCall_Proc xmlns=\"http://tempuri.org/\">\n" 
         "<xmlStr>%@</xmlStr>\n" 
         "<m_LoginId>Username</m_LoginId>\n" 
         "<m_pass>Password</m_pass>\n" 
         "</GetVehiclesByPhone_ServiceCall_Proc>\n" 
         "</soap:Body>\n" 
         "</soap:Envelope>", xmlString]; 

       NSLog(@"%@", soapMessage); 

       NSURL *url = [NSURL URLWithString:@"http://103.9.23.21/TPL_Web_Services/TPLInterface.asmx"]; 
       NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url]; 
       NSString *msgLength = [NSString stringWithFormat:@"%d", [soapMessage length]]; 

       [theRequest addValue: @"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"]; 
       [theRequest addValue: @"http://tempuri.org/GetVehiclesByPhone_ServiceCall_Proc" forHTTPHeaderField:@"SOAPAction"]; 
       [theRequest addValue: msgLength forHTTPHeaderField:@"Content-Length"]; 
       [theRequest setHTTPMethod:@"POST"]; 
       [theRequest setHTTPBody: [soapMessage dataUsingEncoding:NSUTF8StringEncoding]]; 

       NSURLResponse *response; 
       NSError *error; 
       NSData *webData = [NSURLConnection sendSynchronousRequest:theRequest returningResponse:&response error:&error]; 

       if (webData != nil) { 
        NSString *data = [[NSString alloc] initWithData:webData encoding:NSUTF8StringEncoding]; 
        NSLog(@"Data: %@", data); 
        if (error == nil) { 
         if ([data isEqualToString:@"True"] || [data isEqualToString:@"true"]) { 
          [self showSuccessfulAlert]; 
          [self storeDefaults]; 
          [self.dataLayer notifySplashRemoved]; 
         } 
         else { 
          /*UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Invalid Data!" message:@"The phone no. you entered is not registered." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil]; 
          [alert show];*/ 
          sendSMS = YES; 
         } 
        } 
        else { 
         sendSMS = YES; 
        } 
       } 
       else { 
        sendSMS = YES; 
       } 

回答

0

您需要設置Content-Length正確:內容長度是以字節爲單位的體數據的長度,而不是NSString的,這是UTF-16編碼單元的數量的長度:

NSData* body = [soapMessage dataUsingEncoding:NSUTF8StringEncoding]; 
NSString* bodyLength = [NSString stringWithFormat:@"%d", [body count]]; 
[theRequest addValue: bodyLength forHTTPHeaderField:@"Content-Length"]; 
[theRequest setHTTPBody:body]; 
... 
+0

做起來難。仍然得到空的迴應。 –

+0

現在,您應該切換到使用'NSURLConnection'實現代表的方法。這使您能夠更徹底地進行調試,並找出可能的問題。 NSLog的,就像你可以 - 尤其是導致代碼和* HTTP響應代碼*和*任何HTTP響應體*。大量地將斷言宏放入你的代碼中,並逐步完成。 – CouchDeveloper

+0

也嘗試過。我收到的數據在didfinishloading中爲空。 –