ios 调用webserver soap验证 实现

来源:互联网 发布:外贸原单淘宝店 编辑:程序博客网 时间:2024/04/30 06:58

//

//  LogonViewController.m

//  IManager

//

//  Created by remote roamer on 11-11-22.

//  Copyright (c) 2011年 __MyCompanyName__. All rights reserved.

//


#import "LogonViewController.h"



@implementation LogonViewController


@synthesize userNameTextField;

@synthesize userPasswordTextField;

@synthesize userLogonButton;

@synthesize webServiceURL;

@synthesize xmlParser;

@synthesize soapResults;



-(IBAction) logonButtonClick:(id)sender

{

    logonResult = false;

    NSString *soapMessage = [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/\">"

                             "<soap:Body>\n"

                             "<ns1:userLogon xmlns:ns1=\"http://localhost:8080/SampleWebService/webservice/HelloWorld/\">"

                             "<arg0>%@</arg0>"

                             "<arg1>%@</arg1>"

                             "</ns1:userLogon>"

                             "</soap:Body>\n"

                             "</soap:Envelope>",self.userNameTextField.text,self.userPasswordTextField.text];

    NSLog(@"调用webserivce的字符串是:%@",soapMessage);

    //请求发送到的路径

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

    NSURL *url = [NSURL URLWithString:@"http://localhost:8080/SampleWebService/webservice/HelloWorld/"];

    NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:url];

    

     //以下对请求信息添加属性前四句是必有的,

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

    [urlRequest addValue: @"http://localhost:8080/SampleWebService/webservice/HelloWorld" forHTTPHeaderField:@"SOAPAction"];

    [urlRequest addValue: msgLength forHTTPHeaderField:@"Content-Length"];

    [urlRequest setHTTPMethod:@"POST"];

    [urlRequest setHTTPBody: [soapMessage dataUsingEncoding:NSUTF8StringEncoding]];

    


    //请求

    NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:urlRequest delegate:self];

    theConnection = nil;

       

}


//如果调用有错误,则出现此信息

-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error

{

    NSLog(@"ERROR with theConenction");

    UIAlertView * alert =

        [[UIAlertView alloc]

             initWithTitle:@"提示"

             message:[error description]

             delegate:self

             cancelButtonTitle:nil

             otherButtonTitles:@"OK", nil]; 

    [alert show];

}


//调用成功,获得soap信息

-(void) connection:(NSURLConnection *) connection didReceiveData:(NSData *)responseData

{

    NSString * returnSoapXML = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];

    NSLog(@"返回的soap信息是:%@",returnSoapXML);

    //开始解析xml

    xmlParser = [[NSXMLParser alloc] initWithData: responseData];

    [xmlParser setDelegate:self];

    [xmlParser setShouldResolveExternalEntities: YES];

    [xmlParser parse];

    if(logonResult){

        UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"登录成功" message:returnSoapXML delegate:nil cancelButtonTitle:@"ok" otherButtonTitles: nil];

        [alert show];

    }

   

}


//如果soap的返回字符串比较多。需要实现以下这个方法,配合 didReceiveData 方法来正确的接受到所有的soap字符串

//原因是:如果soap的返回字符串比较多。didReceiveData 这个方法会多次被调用。如果把soap解析的功能直接写在didReceiveData这个方法里面。会出现错误。这个时候,就需要 和connectionDidFinishLoading 联用。实现思路是:定义一个类变量NSMutableString * returnSoapXML;用于存放返回的soap字符串。

//一旦有返回内容,获得soap信息,追加到结果字符串中

//-(void) connection:(NSURLConnection *) connection didReceiveData:(NSData *)responseData

//{

//    [returnSoapXML appendString:[[NSMutableString alloc] initWithData:responseData encoding:NSUTF8StringEncoding]];

//}

//最后在connectionDidFinishLoading 方法中实现,对完整的soap字符串的业务处理


//数据全部接受成功以后调用

/*

- (void)connectionDidFinishLoading:(NSURLConnection *)connection

{

    NSLog(@"从远程ws中调用获得客户简单信息的调用成功!");    

    NSLog(@"返回的soap信息是:%@",returnSoapXML);

    //从soap 信息中解析出CusotmerInfo对象数组,并且保存到数据库中

    NSLog(@"开始保存ws返回的内容到本地数据库");

    [[[SoapRtnJsonParser alloc] init] parse2CustomersInfo:[returnSoapXML dataUsingEncoding:NSUTF8StringEncoding]];

}

*/


-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string

{

    

    NSLog(@"返回的soap内容中,return值是: %@",string);

    if ([string isEqualToString:@"true"])

    {

        logonResult = YES;

    }else{

        logonResult = NO;

    }

}






- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

{

    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

    if (self) {

        // Custom initialization

    }

    return self;

}


- (void)didReceiveMemoryWarning

{

    // Releases the view if it doesn't have a superview.

    [super didReceiveMemoryWarning];

    

    // Release any cached data, images, etc that aren't in use.

}


#pragma mark - View lifecycle


- (void)viewDidLoad

{

    [super viewDidLoad];

    // Do any additional setup after loading the view from its nib.

}


- (void)viewDidUnload

{

    [super viewDidUnload];

    // Release any retained subviews of the main view.

    // e.g. self.myOutlet = nil;

}


- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation

{

    // Return YES for supported orientations

    return (interfaceOrientation == UIInterfaceOrientationPortrait);

}


@end


0 0
原创粉丝点击