2013-12-18 193 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.......