AFNetWorking请求WebService

82487712 2015-06-15

.h

#import<Foundation/Foundation.h>

typedefvoid(^SuccessBlock)(NSString*dataString);

typedefvoid(^FialdBlock)(NSString*dataString);

@interfaceHttpRequest:NSObject

+(void)requestWtihParams:(NSDictionary*)params

withSuccessBlock:(SuccessBlock)successBlock

wihtFiladBlock:(FialdBlock)fialdBlock;

@end

.m

#import"HttpRequest.h"

#import"AFNetworking.h"

@implementationHttpRequest

+(void)requestWtihParams:(NSDictionary*)paramswithSuccessBlock:(SuccessBlock)successBlockwihtFiladBlock:(FialdBlock)fialdBlock{

//使用af一般请求

/*AFHTTPRequestOperationManager*manager=[[AFHTTPRequestOperationManageralloc]init];

manager.responseSerializer.acceptableContentTypes=[NSSetsetWithObjects:@"application/json",@"text/html",@"text/json",@"text/javascript",nil];

[managerPOST:@"http://www.weather.com.cn/data/sk/101010100.html"parameters:paramssuccess:^(AFHTTPRequestOperation*operation,idresponseObject){

if(successBlock!=nil){

successBlock(responseObject);

}

}failure:^(AFHTTPRequestOperation*operation,NSError*error){

if(fialdBlock!=nil){

fialdBlock(@"error");

}

}];

http://192.168.0.100/crmpack/Service/DataServiceJson.asmx

PrintingdescriptionofmethodName:

LoginBeforeInit

PrintingdescriptionofcmdJson:

{"parameters":{"versionType":"3"}}

*/

//使用af封装webservice请求

//注意,这里的soapMessage要根据公司服务端定义而来

NSString*methodName=@"LoginBeforeInit";

NSString*p=@"{\"parameters\":{\"versionType\":\"3\"}}";

NSString*soapMessage=[NSStringstringWithFormat:@"<?xmlversion=\"1.0\"?><soap:Envelopexmlns:soap=\"http://www.w3.org/2003/05/soap-envelope\"xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\"xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"xmlns:DataServiceJson=\"http://tempuri.org/\"xsl:version=\"1.0\">"

"<soap:Body><DataServiceJson:ExcuteBll>"

"<DataServiceJson:bllName>%@</DataServiceJson:bllName>"

"<DataServiceJson:cmdJson><![CDATA[%@]]></DataServiceJson:cmdJson>"

"</DataServiceJson:ExcuteBll></soap:Body></soap:Envelope>",methodName,p];

NSURL*url=[NSURLURLWithString:@"http://192.168.0.100/crmpack/Service/DataServiceJson.asmx"];

NSMutableURLRequest*request=[NSMutableURLRequestrequestWithURL:url];

NSString*msgLength=[NSStringstringWithFormat:@"%lu",(unsignedlong)[soapMessagelength]];

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

[requestaddValue:msgLengthforHTTPHeaderField:@"Content-Length"];

[requestsetHTTPMethod:@"POST"];

[requestsetHTTPBody:[soapMessagedataUsingEncoding:NSUTF8StringEncoding]];

AFHTTPRequestOperation*operation=[[AFHTTPRequestOperationalloc]initWithRequest:request];

operation.responseSerializer.acceptableContentTypes=[NSSetsetWithObject:@"application/soap+xml"];

[operationsetCompletionBlockWithSuccess:^(AFHTTPRequestOperation*operation,idresponseObject){

if([responseObjectisKindOfClass:[NSDataclass]]){

NSString*str=[[NSStringalloc]initWithData:responseObjectencoding:NSUTF8StringEncoding];

NSString*startflag=@"<ExcuteBllResult>";

NSString*endflag=@"</ExcuteBllResult>";

NSRangestartRange=[strrangeOfString:startflag];

NSRangeendRange=[strrangeOfString:endflag];

str=[strsubstringWithRange:NSMakeRange(startRange.location+startflag.length,endRange.location-startRange.location-startflag.length)];

NSLog(@"success:%@",str);

successBlock(str);

}

}failure:^(AFHTTPRequestOperation*operation,NSError*error){

NSLog(@"faild:%@",error);

successBlock(error.domain);

}];

[operationstart];

}

@end

相关推荐

倾城一屁博妹一笑 / 0评论 2014-01-15