1. 程式人生 > >iOS webservice+soap

iOS 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]];

伸手黨請直接移步至 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