首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > Web前端 >

iPhone调用Web Service示范

2012-08-31 
iPhone调用Web Service示例因为有些Web Service参数要求有顺序,所以参数使用了NSArray?+ (NSData *) SoapC

iPhone调用Web Service示例

因为有些Web Service参数要求有顺序,所以参数使用了NSArray

?

+ (NSData *) SoapCall:(NSString *)method??PostData:(NSString *)post{????NSData *postData = [post dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:NO];????NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];????[postData autorelease];????????NSString *soapAction = [NSString stringWithFormat:@"%s/%@",SoapPrefix , method??];????????NSURL *url=[[NSURL alloc]initWithString:[ [ NSString alloc ] initWithUTF8String: SoapURL ] ];????NSMutableURLRequest??*request=[[NSMutableURLRequest alloc]init];????????[request setTimeoutInterval: 10 ];????[request setCachePolicy:NSURLRequestReloadIgnoringCacheData];????[request setURL: url ] ;????[request setHTTPMethod:@"POST"]; ????[request setValue:@"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];??????[request setValue:soapAction forHTTPHeaderField:@"SOAPAction"];????????[request setValue:postLength forHTTPHeaderField:@"Content-Length"];????[request setHTTPBody:postData];??????????????NSError *err=nil;????NSData *data=[NSURLConnection sendSynchronousRequest:request?????????????????????????????????????? returningResponse:nil ?????????????????????????????????????????????????? error:&err];????return data ;}+ (NSData *)SoapInvoke:(NSString *)method params:(NSArray *)params{????NSMutableString * post = [[ NSMutableString alloc ] init ] ;????[ post autorelease ];????????[ post appendString:???? @"<?xml version="1.0" encoding="utf-8"?>\n" ???? "<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/""???? " xmlns:xsd="http://www.w3.org/2001/XMLSchema""???? " xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">\n"???? "<soap:Body>\n" ];????????[ post appendString:@"<"];????[ post appendString:method];????[ post appendString:@">\n"];????????for (NSDictionary *dict in params)????{????????NSString *value = nil;????????????????NSString *key = [[dict keyEnumerator] nextObject];????????????????if (key != nil)????????{????????????value = [dict valueForKey:key];????????????????????????[ post appendString:@"<"];????????????[ post appendString:key];????????????[ post appendString:@">"];????????????if( value != nil )????????????{????????????????[ post appendString:value];????????????}????????????else????????????{????????????????[ post appendString:@""];????????????}????????????????????????[ post appendString:@"</"];????????????[ post appendString:key];????????????[ post appendString:@">\n"];????????}????}????????[ post appendString:@"</"];????[ post appendString:method];????[ post appendString:@">\n"];????????[ post appendString:???? @"</soap:Body>\n"???? "</soap:Envelope>\n"???? ];????????//????NSString *filePath = [[NSBundle mainBundle] pathForResource:@"login" ofType:@"json"];????//????NSString *test = [NSString stringWithContentsOfFile:filePath];????NSLog(@"====================request=========================\n%@\n", post);????????return [self SoapCall:method PostData:post];}
? 1 楼 jxufezbz 2012-02-06   这个怎么调用呢?新手求答

热点排行