肥皂反应空ios

时间:2013-11-04 10:15:54

标签: ios soap response nsdata

我发送肥皂请求。但是没有返回数据。 Web服务不会返回任何错误。我无法弄清楚出了什么问题。它与UTF8编码有关吗?请帮忙。以下是我的代码:

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;
                }

1 个答案:

答案 0 :(得分: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];
...