Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Mobile Development > BOOK: Beginning iOS 4 Application Development
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
BOOK: Beginning iOS 4 Application Development
This is the forum to discuss the Wrox book Beginning iOS 4 Application Development by Wei-Meng Lee; ISBN: 978-0-470-91802-9
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning iOS 4 Application Development section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old February 10th, 2011, 02:24 PM
Registered User
 
Join Date: Feb 2011
Posts: 2
Thanks: 1
Thanked 0 Times in 0 Posts
Default Consuming a web service in your iPhone app

Hello guys,

I'm doing chapter 16 - web services

In the book a web service address(www.ecubicle.net/iptocountry.asmx) is given but unfortunly its not available.

So i'm using the following one: http://www.w3schools.com/webservices...nheitToCelsius

the problem is that the soap response is giving me the following error: Server did not recognize the value of HTTP Header SOAPAction: http://www.w3schools.com/webservices...nheitToCelsius.

Code:
<?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><soap:Fault><faultcode>soap:Client</faultcode><faultstring>Server did not recognize the value of HTTP Header SOAPAction: http://www.w3schools.com/webservices/tempconvert.asmx?op=FahrenheitToCelsius.</faultstring><detail /></soap:Fault></soap:Body></soap:Envelope>
Why is a dot at the end of the service ? maybe can be this causing my problem.

Take a look above at my code and soap request..

CODE:

Code:
-(IBAction)btnConvertToCelsius:(id)sender{

	//NSString *soapMsg = [NSString stringWithFormat:@"<?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://www.w3.org/2003/05/soap-envelope\">"
	//					 "<soap:Body>"
	//					 "<FahrenheitToCelsius xmlns=\"http://tempuri.org/\">"
	//					 "<Fahrenheit>%@</Fahrenheit>"
	//					 "</FahrenheitToCelsius>"
	//					 "</soap:Body>"
	//					 "</soap:Envelope>", value_.text];
	
	
	NSString *soapMsg = [NSString stringWithFormat:@"<?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>"
						 "<FahrenheitToCelsius xmlns=\"http://tempuri.org/\">"
						 "<Fahrenheit>%@</Fahrenheit>"
						 "</FahrenheitToCelsius>"
						 "</soap:Body>"
						 "</soap:Envelope>", value_.text];
	
	
	
	NSLog(@"%@",soapMsg);
	
	NSURL *url = [NSURL URLWithString:@"http://www.w3schools.com/webservices/tempconvert.asmx"];
	
	NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url];
	
	NSString *msgLenght = [NSString stringWithFormat:@"%d", [soapMsg length]];
	
	[req addValue:@"tet/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
	
	
	[req addValue:@"http://www.w3schools.com/webservices/tempconvert.asmx?op=FahrenheitToCelsius" forHTTPHeaderField:@"soapaction"];
	
	[req addValue:msgLenght forHTTPHeaderField:@"Content-Lenght"];
		
	[req setHTTPMethod:@"POST"];
	
	[req setHTTPBody:[soapMsg dataUsingEncoding:NSUTF8StringEncoding]];
	
	[activityIndicator startAnimating];
	
	conn = [[NSURLConnection alloc] initWithRequest:req delegate:self];
	
	if (conn) {
		webData = [[NSMutableData data] retain];
	}
	
}

-(void) connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *) response{

	[webData setLength:0];
}

-(void) connection:(NSURLConnection *)connection didReceiveData:(NSData *) data{
	
	[webData appendData:data];
}

-(void) connection:(NSURLConnection *)connection didFailWithError:(NSError *) error{
	
	[webData release];
	[connection release];
}

-(void) connectionDidFinishLoading:(NSURLConnection *)connection{
	
	NSLog(@"DONE. Received Bytes:%d",[webData length]);
	
	NSString *theXml = [[NSString alloc] initWithBytes:[webData mutableBytes] length:[webData length] encoding:NSUTF8StringEncoding];
	
	NSLog(@"%@",theXml);
	[theXml release];
	
	[activityIndicator stopAnimating];
	
	[connection release];
	
	[webData release];
}

SOAP REQUEST:

Code:
<?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><FahrenheitToCelsius xmlns="http://tempuri.org/"><Fahrenheit>55</Fahrenheit></FahrenheitToCelsius></soap:Body></soap:Envelope>
Reply With Quote
  #2 (permalink)  
Old February 11th, 2011, 12:45 AM
Friend of Wrox
Points: 1,671, Level: 16
Points: 1,671, Level: 16 Points: 1,671, Level: 16 Points: 1,671, Level: 16
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2010
Posts: 298
Thanks: 1
Thanked 62 Times in 60 Posts
Default

You have a few errors that need to be corrected.

[req addValue:@"tet/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"]; //typo text/xml

[req addValue:@"http://www.w3schools.com/webservices/tempconvert.asmx?op=FahrenheitToCelsius" forHTTPHeaderField:@"soapaction"];//incorrect should be
[req addValue:@"http://tempuri.org/FahrenheitToCelsius" forHTTPHeaderField:@"SOAPAction"];

As you can see from the page in your link
POST /webservices/tempconvert.asmx HTTP/1.1
Host: www.w3schools.com
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://tempuri.org/FahrenheitToCelsius" This is the SOAPAction

[req addValue:msgLenght forHTTPHeaderField:@"Content-Lenght"]; // typo "Content-Length"

For working examples of the WebService refer to

Problem with web service example chapter 14

I have made two working examples of Web Services available including a Temperature Converter. Check for download links on a January 20 post from thepianoguy

These corrections should resolve your request error.
Bob
Reply With Quote
The Following User Says Thank You to thepianoguy For This Useful Post:
dapinheiro (February 11th, 2011)
  #3 (permalink)  
Old February 11th, 2011, 06:17 AM
Registered User
 
Join Date: Feb 2011
Posts: 2
Thanks: 1
Thanked 0 Times in 0 Posts
Default

Bob,

I've download your code, but i was able to receive a response from soap with your corrections ! Many thanks
Reply With Quote
  #4 (permalink)  
Old February 11th, 2011, 08:44 AM
Friend of Wrox
Points: 1,671, Level: 16
Points: 1,671, Level: 16 Points: 1,671, Level: 16 Points: 1,671, Level: 16
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2010
Posts: 298
Thanks: 1
Thanked 62 Times in 60 Posts
Default

Glad it worked. I didn't actually compile your code, just did a visual inspection, so I could have missed something.
The projects I did for the other forum (different book by the same author, identical project and, annoyingly, same lack of functionality that should have been addressed on the forum or in an errata, since it was pointed out months before this book was released) also include a working ipToCountry, extension of use of the xmlParser and implementations of all versions of the requests (SOAP 1.1, SOAP 1.2, HTTP_GET and HTTP_POST) when available.
I probably got more out the Chapter since project didn't work, since I had to find out a way to make it work. It seems you had the same mindset in looking for alternatives. However, I would prefer it if the Chapter project did work, and finding, and implementing another service was followup exercise or challenge.
Bob
Reply With Quote
  #5 (permalink)  
Old May 30th, 2013, 11:40 AM
Registered User
Points: 3, Level: 1
Points: 3, Level: 1 Points: 3, Level: 1 Points: 3, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: May 2013
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Same code updated with blocks

I'm posting this code as I found this link useful. There are a few syntax errors, in the original answer.

Code:
        NSString *soapMessage = //form the soap envelope
    
        NSURL *url = [NSURL URLWithString:@"http://www.w3schools.com/webservices/tempconvert.asmx"];
	
	NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:url];
	
	[urlRequest addValue:@"text/xml"
          forHTTPHeaderField:@"Content-Type"];
	    
	[urlRequest setHTTPMethod:@"POST"];
	
	[urlRequest setHTTPBody:[soapMessage
                 dataUsingEncoding:NSUTF8StringEncoding]];
    
       [NSURLConnection sendAsynchronousRequest:urlRequest 
                                                            queue:[NSOperationQueue mainQueue] 
                                           completionHandler:^(NSURLResponse *response, 
                                                                        NSData *responseData,
                                                                        NSError *error) 
       {
        
           NSString *responseString = [[NSString alloc]initWithData:responseData
                                                                             encoding:NSUTF8StringEncoding];
        
           NSLog(@"%@",responseString);
        
       }];

Last edited by Anupdas; May 30th, 2013 at 11:44 AM. Reason: Improved formatting
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Trouble consuming doc literal web service ... mussitsch Classic ASP XML 0 March 4th, 2009 02:43 PM
Consuming Web service with NuSoap CraigWhitfield PHP How-To 0 February 27th, 2008 11:20 AM
Consuming Web Service edgtr .NET Web Services 0 November 1st, 2006 03:50 PM
Consuming web service with dismal performance jcrossett ASP.NET 2.0 Professional 0 December 13th, 2005 11:19 AM



All times are GMT -4. The time now is 10:29 PM.


Powered by vBulletin®
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.