iOS webservice+soap

来源:互联网 发布:ubuntu安装 分区 编辑:程序博客网 时间:2024/06/09 23:22

本文章采用的字符串常量:
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]];

伸手党请直接移步至 version:3.0

ASIHTTPRequest
这个东西已经很多年没更新了,相信在用的兄弟已经很少了吧?
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];


2013-12-05
version:1.0
AFNetworking 2.0
之前写的代码有问题,因为webxml这个网站支持GET、POST和SOAP协议,所以在接收到回执的时候误以为是SOAP请求成功,但仔细看了一下返回信息发现是普通的POST请求成功的消息。以下是错误的代码
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);
}];


2013-12-05
version:2.0
AFNetworking 2.0
在发现错误后,本人调试跟踪了一下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);
}];


日期不详
version:2.1
AFNetworking 2.x
闲着没事把之前的代码拿来又看了一遍,想着能不能在不改动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];


2014-10-23
version:3.0
AFNetworking 2.4.1
显然,以上的解决方法不是最优的,由于我的强迫症,代码体积看着实在不爽。这一次,又踏上了AFNetworking的调试跟踪之路。我们为什么要自己写NSURLRequest呢?这一步能不能省略掉呢?答案是肯定的:
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.responseSerializer = [[AFHTTPResponseSerializer alloc] init];
[manager.requestSerializer setValue:@"application/soap+xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[manager.requestSerializer setQueryStringSerializationWithBlock:^NSString *(NSURLRequest *request, NSDictionary *parameters, NSError *__autoreleasing *error) {
    return soapMessage;
}];
[manager POST:@"http://www.webxml.com.cn/WebServices/WeatherWebService.asmx" parameters:soapMessage 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);
}];


参考资料
http://blog.csdn.net/iamstillzhang/article/details/8264049