因为有的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:
@"\n"
"\n"
"\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:@"\n"];
}
}
[ post appendString:@"\n"];
[ post appendString:
@"\n"
"\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];
}