没有得到iPhone上服务器的响应

时间:2013-05-20 11:59:54

标签: iphone ios objective-c nsurlconnection

我是iPhone编程新手。使用下面的代码我向服务器发送请求,但我没有得到输出。谁能告诉我这段代码中的错误是什么?

NSString *soapMessage=[NSString stringWithFormat:
                           @"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
                           "<soap:Envelope \n"
                           "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" \n"
                          "xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"\n"
                           "xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope\">\n"
                           "<soap:Body>\n"
                           "<OnlineStatus xmlns=\"http://tempuri.org\">\n"
                           "<CafeName>CyberCafeName</CafeName>\n"
                           "<FromDate>9/4/2012</FromDate>\n"
                           "<ToDate> 5/5/2013</ToDate>\n"
                           "</OnlineStatus>\n"
                           "</soap:Body>\n"
                           "</soap:Envelope>"];
    NSLog(@"%@",soapMessage);
    NSURL *url = [NSURL URLWithString:@"http://www.ebidmanagerdemo.com/gjHouseOnline/xml/XMLDataService.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/OnlineStatus" forHTTPHeaderField:@"Soapaction"];
    [theRequest addValue: msgLength forHTTPHeaderField:@"Content-Length"];
    [theRequest setHTTPMethod:@"POST"];
    [theRequest setHTTPBody: [soapMessage dataUsingEncoding:NSUTF8StringEncoding]];
    NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];

    if(theConnection) {
        webData = [NSMutableData data];
    }
    else {
        NSLog(@"theConnection is NULL");

    NSString *msgLength = [NSString stringWithFormat:@"%d", [soapMessage length]];
    NSLog(@"%@",msgLength);

我在控制台中收到此输出错误消息:

 2013-05-20 17:28:26.447 NewC Newcafezee[1166:11303] <?xml version="1.0" encoding="utf-8"?>
<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">
<soap:Body>
<OnlineStatus xmlns="http://tempuri.org">
<CafeName>CyberCafeName</CafeName>
<FromDate>9/4/2012</FromDate>
<ToDate> 5/5/2013</ToDate>
</OnlineStatus>
</soap:Body>
</soap:Envelope>
2013-05-20 17:28:28.355 NewC Newcafezee[1166:11303] <?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Header><soap12:Upgrade xmlns:soap12="http://www.w3.org/2003/05/soap-envelope"><soap12:SupportedEnvelope qname="soap:Envelope" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" /><soap12:SupportedEnvelope qname="soap12:Envelope" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope" /></soap12:Upgrade></soap:Header><soap:Body><soap:Fault><faultcode>soap:VersionMismatch</faultcode><faultstring>System.Web.Services.Protocols.SoapException: Possible SOAP version mismatch: Envelope namespace http://schemas.xmlsoap.org/soap/envelope was unexpected. Expecting http://schemas.xmlsoap.org/soap/envelope/.
   at System.Web.Services.Protocols.SoapServerProtocol.CheckHelperVersion()
   at System.Web.Services.Protocols.SoapServerProtocol.ReadParameters()
   at System.Web.Services.Protocols.WebServiceHandler.CoreProcessRequest()</faultstring><detail /></soap:Fault></soap:Body></soap:Envelope>
2013-05-20 17:28:28.357 NewC Newcafezee[1166:11303] fsdfsd  

此原始肥皂请求

POST /gjHouseOnline/xml/XMLDataService.asmx HTTP/1.1
Host: www.ebidmanagerdemo.com
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://tempuri.org/OnlineStatus"

<?xml version="1.0" encoding="utf-8"?>
<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/">
  <soap:Body>
    <OnlineStatus xmlns="http://tempuri.org/">
      <CafeName>string</CafeName>
      <FromDate>dateTime</FromDate>
      <ToDate>dateTime</ToDate>
    </OnlineStatus>
  </soap:Body>
</soap:Envelope>

1 个答案:

答案 0 :(得分:1)

我刚刚更新了行

"xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope\">\n" 

"xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">\n"

并给予我回复

 <?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><OnlineStatusResponse xmlns="http://tempuri.org/"><OnlineStatusResult /></OnlineStatusResponse></soap:Body></soap:Envelope>

我接受它的空,但至少它没有错误。数据可能不适用于指定的参数。