ios 调用webservice 辅助类
类还在修改中,现只是用控制台进行调试。之后再加上结果的解析。初学objc几天,这里只作学习笔记。欢迎指正.不是不写备注,是不喜欢写备注。
// // bsoap.h // webServiceTest // // Created by 国栋 谢 on 12-10-4. // Copyright 2012年 __MyCompanyName__. All rights reserved. // #import <Foundation/Foundation.h> @interface Bsoap : NSObject{ // ws NSMutableURLRequest *request; NSString *soapParas; NSString *soapBody1; NSString *soapBody2; } @property (copy) NSString *nameSapce; @property (copy) NSString *endPoint; @property (copy) NSString *soapAction; @property (nonatomic, retain) NSError *error; - (void)SetMethod:(NSString *)strName; - (void)AddParam:(NSString *)strvalue Keys:(NSString *)strKey; - (NSString *)CnnWebService; - (id)initWithNameSpaceAndEndPoint:(NSString *) strnameSapce StrEndPoint:(NSString *)strEndPoint; // xml @end
// // bsoap.m // webServiceTest // // Created by 国栋 谢 on 12-10-4. // Copyright 2012年 __MyCompanyName__. All rights reserved. // #import "Bsoap.h" @implementation Bsoap @synthesize nameSapce; @synthesize endPoint; @synthesize soapAction; @synthesize error; - (id)init { self = [super init]; if (self) { // Initialization code here. nameSapce = @"http://tempuri.org/"; endPoint = @"http://xx.asmx"; soapParas =[[NSMutableString alloc] init]; } return self; } - (id)initWithNameSpaceAndEndPoint:(NSString *) strnameSapce StrEndPoint:(NSString *)strEndPoint { self = [super init]; if (self) { // Initialization code here. nameSapce =strnameSapce; endPoint =strEndPoint; soapParas =[[NSMutableString alloc] init]; } return self; } - (void)SetMethod:(NSString *)strName { soapAction =[[NSString alloc] initWithString:[nameSapce stringByAppendingFormat:strName]]; // NSLog(@"sa :%@",soapAction); soapBody1 = [[NSString alloc] initWithFormat: @"<?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>" "<%@ xmlns=\"%@\">\n",strName,nameSapce]; soapBody2 = [[NSString alloc] initWithFormat: @"</%@>\n" "</soap:Body>\n" "</soap:Envelope>",strName]; //NSLog(@"%@",soapMessage); NSURL *url = [NSURL URLWithString:endPoint]; request = [NSMutableURLRequest requestWithURL:url]; } - (void)AddParam:(NSString *)strvalue Keys:(NSString *)strKey { soapParas = [soapParas stringByAppendingFormat:@"<%@>%@</%@>\n", strKey, strvalue, strKey]; } - (NSString *)CnnWebService { NSURLResponse *response; error = nil; NSString *soapMessage = [soapBody1 stringByAppendingFormat:@"%@%@",soapParas,soapBody2]; NSString *msgLength = [NSString stringWithFormat:@"%d",[soapMessage length]]; [request addValue:msgLength forHTTPHeaderField:@"Content-Length"]; [request addValue:@"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"]; [request addValue:soapAction forHTTPHeaderField:@"SOAPAction"]; [request setHTTPBody:[soapMessage dataUsingEncoding:NSUTF8StringEncoding]]; [request setHTTPMethod:@"POST"]; NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error]; NSString *result = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding]; NSRegularExpression *regex =[NSRegularExpression regularExpressionWithPattern:@"<.*?>" options:0 error:nil]; NSString *str = [regex stringByReplacingMatchesInString:result options:0 range:NSMakeRange(0, result.length) withTemplate:@""]; // NSLog(@"%@",result); return str; } @end
调用:我别外写了一个WSClient类,对应Web端的方法。
Bsoap *bsoap = [[Bsoap alloc]initWithNameSpaceAndEndPoint:@"http://yourNamespace" StrEndPoint:@"http://xxx.asmx"];
[bsoapSetMethod:@"SearcheBook"];
[bsoapAddParam:@".net"Keys:@"keyWord"];
[bsoap AddParam:@"2" Keys:@"pageNo"];
NSString *strsoap = [bsoap CnnWebService];
这样就完成一次调用了。先创建Bsoap对象,然后设置方法,添加参数,多个就添加多次。然后调用CnnWebService。就可以返回结果。