菜单

Objective-C Soap

2015年01月9日 - objective-c

https://github.com/iPolaris/SoapRequest
http://www.codeproject.com/Tips/622376/iOS-Soap-Webservice-Calling-and-Parsing-the-Resp

//Import the frameworks header to your ViewController header file :
#import "ASIHTTPRequest.h"
#import "ASIFormDataRequest.h"
#import "SBJson.h" 

//Unfortunately, there is no framework to create a SOAP message for us, so we will create the SOAP message as follows (this is the routine) :
- (IBAction)btnConvert:(id)sender {
   
    NSString *soapMessage = [NSString stringWithFormat:@"\n"
         "\n"
         "\n"
         " \n"
         "%@\n"
         "\n"
         "\n"
         "\n" ,textFieldCelcisus.text];
    
    
    NSURL *url = [NSURL URLWithString:@"http://w3schools.com/webservices/tempconvert.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/CelsiusToFahrenheit" 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] ;
        NSLog(@"Problem");
    }
    else
    {
        NSLog(@"theConnection is NULL");
    }
       
    
}// end of buton convert


//After making our request to the server, we need to catch the server response, for this. By using the connectionDidFinishLoading method, we catch the service response.
-(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
{
    NSLog(@"ERROR with theConenction");
   
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    NSLog(@"DONE. Received Bytes: %d", [webData length]);
    NSString *theXML = [[NSString alloc] initWithBytes: 
      [webData mutableBytes] length:[webData length] encoding:NSUTF8StringEncoding];
}


//Now we got the data in XML form, but we need to parse this XML to get our fahrenheit value. To do that, we need to add NSXMLParser's delegate to our viewController:
@interface SEViewController : UIViewController  

//In connectionDidFinishLoading method, we create an NSXMLParser object and pass the server response to it:
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    NSLog(@"DONE. Received Bytes: %d", [webData length]);
    NSString *theXML = [[NSString alloc] initWithBytes: 
      [webData mutableBytes] length:[webData length] encoding:NSUTF8StringEncoding];
   
    NSData *myData = [theXML dataUsingEncoding:NSUTF8StringEncoding];
   
    NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithData:myData];
   
    // Don't forget to set the delegate!
    xmlParser.delegate = self;
   
    // Run the parser
    BOOL parsingResult = [xmlParser parse];
   
}

//Finally, we will implement NSXMLParser's delegate methods to get each element:


-(void) parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName 
  namespaceURI:(NSString *)namespaceURI qualifiedName:
    (NSString *)qName attributes:(NSDictionary *)attributeDict
{
    NSString  *currentDescription = [NSString alloc];
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
    curDescription = string;
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName 
  namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
    if([elementName isEqual: @"CelsiusToFahrenheitResult"]);
    textFieldResult.text = curDescription;
} 

发表评论

电子邮件地址不会被公开。 必填项已用*标注