`
jiapumin
  • 浏览: 341259 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

iOS +AFNetworking2.0+webservice+soap

 
阅读更多

本文章采用的字符串常量:

NSString *soapMessage =
@"<?xml version=\"1.0\" encoding=\"utf-8\"?> \n"
"<soap12:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap12=\"http://www.w3.org/2003/05/soap-envelope\">"
"<soap12:Body>"
"<getSupportCity xmlns=\"http://WebXml.com.cn/\">"
"<byProvinceName>ALL</byProvinceName>"
"</getSupportCity>"
"</soap12:Body>"
"</soap12:Envelope>";
NSString *soapLength = [NSString stringWithFormat:@"%d", [soapMessage length]];

 

之前写的代码有问题,因为webxml这个网站支持GET、POST和SOAP协议,所以在接收到回执的时候误以为是SOAP请求成功,但仔细看了一下返回信息发现是普通的POST请求成功的消息。以下是错误的代码

AFNetworking 2.0

AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc] initWithBaseURL:[NSURL URLWithString:@"http://www.webxml.com.cn"]];
manager.responseSerializer = [[AFHTTPResponseSerializer alloc] init];
[manager.requestSerializer multipartFormRequestWithMethod:@"POST" URLString:@"WebServices/WeatherWebService.asmx/getSupportCity" parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
    [formData appendPartWithHeaders:[NSDictionary dictionaryWithObjectsAndKeys:@"application/x-www-form-urlencoded", @"Content-Type", soapLength, @"Content-Length", nil] body:[soapMessage dataUsingEncoding:NSUTF8StringEncoding]];
}];

[manager POST:@"/WebServices/WeatherWebService.asmx/getSupportCity" parameters:@{@"byProvinceName":@"ALL"} success:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSString *response = [[NSString alloc] initWithData:(NSData *)responseObject encoding:NSUTF8StringEncoding];
    NSLog(@"responseObject: %@", response);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"Error: %@", error);
}];

 

在发现错误后,本人调试跟踪了一下AFNetworking2.0的POST过程,发现在添加Content-Type和Content-Length的时候,AFNetworking把字段值改掉了,所以在AFHTTPRequestOperationManager类中添加了如下方法:

- (AFHTTPRequestOperation *)SOAP:(NSString *)URLString
       constructingBodyWithBlock:(void (^)(NSMutableURLRequest *request))block
                         success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
                         failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure
{
    NSMutableURLRequest *request = [self.requestSerializer multipartFormRequestWithMethod:@"POST" URLString:[[NSURL URLWithString:URLString relativeToURL:self.baseURL] absoluteString] parameters:nil constructingBodyWithBlock:nil];
    block(request);
    AFHTTPRequestOperation *operation = [self HTTPRequestOperationWithRequest:request success:success failure:failure];
    [self.operationQueue addOperation:operation];
    
    return operation;
}

 
添加好此方法后,直接调用就可以了

AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc] initWithBaseURL:[NSURL URLWithString:@"http://www.webxml.com.cn"]];
manager.responseSerializer = [[AFHTTPResponseSerializer alloc] init];
[manager.requestSerializer setValue:@"application/soap+xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[manager.requestSerializer setValue:soapLength forHTTPHeaderField:@"Content-Length"];
[manager SOAP:@"/WebServices/WeatherWebService.asmx" constructingBodyWithBlock:^(NSMutableURLRequest *request) {
    [request setHTTPBody:[soapMessage dataUsingEncoding:NSUTF8StringEncoding]];
} success:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSString *response = [[NSString alloc] initWithData:(NSData *)responseObject encoding:NSUTF8StringEncoding];
    NSLog(@"%@, %@", operation, response);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSString *response = [[NSString alloc] initWithData:(NSData *)[operation responseObject] encoding:NSUTF8StringEncoding];
    NSLog(@"%@, %@", operation, error);
}];

 
AFNetworking2.0 + SOAP调用成功!!!

 

闲着没事把之前的代码拿来又看了一遍,想着能不能在不改动AFNetworking代码的前提下调用SOAP协议成功,如果有同样想法的朋友可以试一试下面的代码

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.responseSerializer = [[AFHTTPResponseSerializer alloc] init];
[manager.requestSerializer setValue:@"application/soap+xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[manager.requestSerializer setValue:soapLength forHTTPHeaderField:@"Content-Length"];
NSMutableURLRequest *request = [manager.requestSerializer requestWithMethod:@"POST" URLString:@"http://www.webxml.com.cn/WebServices/WeatherWebService.asmx" parameters:nil];
[request setHTTPBody:[soapMessage dataUsingEncoding:NSUTF8StringEncoding]];
AFHTTPRequestOperation *operation = [manager HTTPRequestOperationWithRequest:request success:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSString *response = [[NSString alloc] initWithData:(NSData *)responseObject encoding:NSUTF8StringEncoding];
    NSLog(@"%@, %@", operation, response);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSString *response = [[NSString alloc] initWithData:(NSData *)[operation responseObject] encoding:NSUTF8StringEncoding];
    NSLog(@"%@, %@", operation, error);
}];
[manager.operationQueue addOperation:operation];}

 
调用成功!

 

ASIHTTPRequest
NSString *soapMessage =
@"<?xml version=\"1.0\" encoding=\"utf-8\"?> \n"
"<soap12:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap12=\"http://www.w3.org/2003/05/soap-envelope\">"
"<soap12:Body>"
"<getSupportCity xmlns=\"http://WebXml.com.cn/\">"
"<byProvinceName>ALL</byProvinceName>"
"</getSupportCity>"
"</soap12:Body>"
"</soap12:Envelope>";
NSString *soapLength = [NSString stringWithFormat:@"%d", [soapMessage length]];

NSURL *url = [NSURL URLWithString:@"http://www.webxml.com.cn/WebServices/WeatherWebService.asmx"];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setDelegate:self];

[request addRequestHeader:@"Content-Type" value:@"application/soap+xml; charset=utf-8"];
[request addRequestHeader:@"Content-Length" value:soapLength];
[request setRequestMethod:@"POST"];
[request appendPostData:[soapMessage dataUsingEncoding:NSUTF8StringEncoding]];
[request setDefaultResponseEncoding:NSUTF8StringEncoding];
[request startAsynchronous];

 

 

参考资料

http://blog.csdn.net/iamstillzhang/article/details/8264049

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics