The code which you gave, helped me alot. But by doing some small changed i got the result.
Here is the Code for consuming a sample Webservice .......Its working great to me....
- (IBAction)convertButton:(id)sender {
// construct envelope (not optimized, intended to show basic steps)
NSString *envelopeText =[NSString stringWithFormat:@"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" "<soap12:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema- to instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap12=\"http://www.w3.org/2003/05/soap-envelope\">\n" " <soap12:Body>\n" " <CelsiusToFahrenheit xmlns=\"http://tempuri.org/\">\n" " <Celsius>%@</Celsius>\n" " </CelsiusToFahrenheit>\n" " </soap12:Body>\n" "</soap12:Envelope>",temperatureTextField.text];
envelopeText = [NSString stringWithFormat:envelopeText, temperatureTextField.text];
NSData *envelope = [envelopeText dataUsingEncoding:NSUTF8StringEncoding];
// construct request
NSString *url = @"http://www.w3schools.com/webservices/tempconvert.asmx";
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:30.0];
[request addValue:@"http://tempuri.org/CelsiusToFahrenheit" forHTTPHeaderField:@"SOAPAction"];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:envelope];
[request setValue:@"application/soap+xml; charset=utf-8"
forHTTPHeaderField:@"Content-Type"];
[request setValue:[NSString stringWithFormat:@"%d", [envelope length]]forHTTPHeaderField:@"Content-Length"];
// fire away
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
if (connection)
responseData = [NSMutableData data];
else
NSLog(@"NSURLConnection initWithRequest: Failed to return a connection.");
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
[responseData setLength:0]; }
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[responseData appendData:data];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
NSLog(@"connection didFailWithError: %@ %@", error.localizedDescription,
[error.userInfo objectForKey:NSURLErrorFailingURLStringErrorKey]);
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
NSLog(@"DONE. Received Bytes:%d",[responseData length]);
NSString *theXml = [[NSString alloc] initWithBytes:[responseData mutableBytes] length:[responseData length] encoding:NSUTF8StringEncoding];
NSLog(@"The final result :%@",theXml);
if(xmlParser)
{
}
xmlParser = [[NSXMLParser alloc] initWithData: responseData];
[xmlParser setDelegate:self];
[xmlParser setShouldResolveExternalEntities:YES];
[xmlParser parse];
}
//---when the start of an element is found---
-(void) parser:(NSXMLParser *) parser didStartElement:(NSString *) elementName namespaceURI:(NSString *) namespaceURI qualifiedName:(NSString *) qName attributes:(NSDictionary *)attributeDict {
if( [elementName isEqualToString:@"CelsiusToFahrenheitResult"])
{
if (!soapResults)
{
soapResults = [[NSMutableString alloc] init];
}
elementFound = YES;
}
}
-(void)parser:(NSXMLParser *) parser foundCharacters:(NSString *)string
{
if (elementFound)
{
[soapResults appendString: string];
}
}
//---when the end of element is found---
-(void)parser:(NSXMLParser *)parser
didEndElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI
qualifiedName:(NSString *)qName
{
if ([elementName isEqualToString:@"CelsiusToFahrenheitResult"])
{
//---displays the country---
NSLog(@"%@",soapResults);
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:@"Fahrenheit Value!"
message:soapResults
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
resultLabel.text=soapResults;
//[alert release];
[soapResults setString:@""];
elementFound = FALSE;
}
}