2013-12-18 100 views
0

我使用SOAP在iPhone应用程序,我得到这个错误:使用Web服务在iPhone

<faultcode>soap:Client</faultcode><faultstring>Server did not recognize the value of HTTP Header SOAPAction: http://tempuri.org/CelsiusToFahrenheit.</faultstring><detail /></soap:Fault></soap:Body></soap:Envelope> 

这是我的代码:

NSString *soapFormat = [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" 
          "<CelsiusToFahrenheit xmlns=\"http://tempuri.org/\">\n" 
          "<Celsius>%@</Celsius>\n" 
          "</CelsiusToFahrenheit>\n" 
          "</soap:Body>\n" 
          "</soap:Envelope>\n",txt1.text]; 


    NSURL *locationOfWebService = [NSURL URLWithString:@"http://www.w3schools.com/webservices/tempconvert.asmx"]; 

    NSLog(@"web url = %@",locationOfWebService); 

    NSMutableURLRequest *theRequest = [[NSMutableURLRequest alloc]initWithURL:locationOfWebService]; 

    NSString *msgLength = [NSString stringWithFormat:@"%d",[soapFormat length]]; 

    [theRequest addValue:@"text/xml" forHTTPHeaderField:@"Content-Type"]; 
    [theRequest addValue:@"http://tempuri.org/CelsiusToFahrenheit" forHTTPHeaderField:@"SOAPAction"]; 
    [theRequest addValue:msgLength forHTTPHeaderField:@"Content-Length"]; 
    [theRequest setHTTPMethod:@"POST"]; 
    //the below encoding is used to send data over the net 
    [theRequest setHTTPBody:[soapFormat dataUsingEncoding:NSUTF8StringEncoding]]; 

任何帮助将不胜感激,谢谢。

+0

打印您的soapFormat串和检查的价值txt1.text正确地在那里。 –

+0

现在检查答案3它为我工作...... –

回答

1

只是通过URL http://www.w3schools.com/webservices/tempconvert.asmx用于Web服务的位置去了,在那里,我发现,当使用SOAP 1.1 SOAP动作是http://www.w3schools.com/webservices/CelsiusToFahrenheit

,而不是http://tempuri.org/CelsiusToFahrenheit怎么写,你soacp行动领域。 这是发送请求到这个特定的Web服务的确切格式,

POST /webservices/tempconvert.asmx HTTP/1.1 
Host: www.w3schools.com 
Content-Type: text/xml; charset=utf-8 
Content-Length: length 
SOAPAction: "http://www.w3schools.com/webservices/CelsiusToFahrenheit" 

我相信你正在设置SOAP动作的错误值。因为提到的肥皂行动与你写的不一样。 (仅供参考,肥皂操作用于识别一个特定网络服务或网络的方法,从一个大的无Web方法,其存在于相同的位置)

+0

@mahesh chowdary洛尔兹,它很搞笑,你在这里试图达到什么,点。 你可以刚刚说改变你的肥皂行动到正确的一个或投票我的答案UP ....我仍然微笑着为什么你浪费你的时间已经给出的答案.. – Rana

+0

@ Rajneesh071上面评论你太兄弟... – Rana

0

尝试this.It将工作

NSString *soapFormat = [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" 
          "<CelsiusToFahrenheit xmlns=\"http://www.w3schools.com/webservices/\">\n" 
          "<Celsius>%@</Celsius>\n" 
          "</CelsiusToFahrenheit>\n" 
          "</soap:Body>\n" 
          "</soap:Envelope>\n",@"30"]; 


    NSURL *locationOfWebService = [NSURL URLWithString:@"http://www.w3schools.com/webservices/tempconvert.asmx"]; 

    NSLog(@"web url = %@",locationOfWebService); 

    NSMutableURLRequest *theRequest = [[NSMutableURLRequest alloc]initWithURL:locationOfWebService]; 

    NSString *msgLength = [NSString stringWithFormat:@"%d",[soapFormat length]]; 

    [theRequest addValue:@"text/xml" forHTTPHeaderField:@"Content-Type"]; 
    [theRequest addValue:@"http://www.w3schools.com/webservices/CelsiusToFahrenheit" forHTTPHeaderField:@"SOAPAction"]; 
    [theRequest addValue:msgLength forHTTPHeaderField:@"Content-Length"]; 
    [theRequest setHTTPMethod:@"POST"]; 
    //the below encoding is used to send data over the net 
    [theRequest setHTTPBody:[soapFormat dataUsingEncoding:NSUTF8StringEncoding]]; 
0

//试试这个,它返回整数值

NSString *soapFormat = [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" 
         "<CelsiusToFahrenheit xmlns=\"http://www.w3schools.com/webservices/\">\n" 
         "<Celsius>%@</Celsius>\n" 
         "</CelsiusToFahrenheit>\n" 
         "</soap:Body>\n" 
         "</soap:Envelope>\n",@"50"]; 


NSURL *locationOfWebService = [NSURL URLWithString:@"http://www.w3schools.com/webservices/tempconvert.asmx"]; 

NSLog(@"web url = %@",locationOfWebService); 

NSMutableURLRequest *theRequest = [[NSMutableURLRequest alloc]initWithURL:locationOfWebService]; 

NSString *msgLength = [NSString stringWithFormat:@"%d",[soapFormat length]]; 

[theRequest addValue:@"text/xml" forHTTPHeaderField:@"Content-Type"]; 
[theRequest addValue:@"http://www.w3schools.com/webservices/CelsiusToFahrenheit" forHTTPHeaderField:@"SOAPAction"]; 
[theRequest addValue:msgLength forHTTPHeaderField:@"Content-Length"]; 
[theRequest setHTTPMethod:@"POST"]; 
//the below encoding is used to send data over the net 
[theRequest setHTTPBody:[soapFormat dataUsingEncoding:NSUTF8StringEncoding]]; 

NSURLResponse *response; 
NSData *data_reply; 
NSError *err; 

    data_reply = [NSURLConnection sendSynchronousRequest:theRequest returningResponse:&response error:&err]; 

    NSString *aStr = [[NSString alloc] initWithData:data_reply encoding:NSASCIIStringEncoding]; 
NSLog(@"aStr:%@",aStr); 

O/P:

<?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><CelsiusToFahrenheitResponse xmlns="http://www.w3schools.com/webservices/"><CelsiusToFahrenheitResult>122</CelsiusToFahrenheitResult></CelsiusToFahrenheitResponse></soap:Body></soap:Envelope> 

    check this answer it returns 122 integer value.......