iOS webservice+soap

原创 2013年12月05日 20:10:24

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

版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

iOS调用WebService(SOAP接口)

首先有几点说在前面 一般,在请求URL的后面带有WSDL字样的需要调用WebService URL样式例子:http://ip:port/navigable/webservice/loginSeric...

ios webservice编程:soap封装调用,返回xml,json解析

webservice调用,包括soap请求与响应,返回(xml,json)解析。 soap请求与响应:ios webservice+soap编程 xml,json解析:ios XML,JSON,数组解...
  • f10_s
  • f10_s
  • 2014-11-27 16:20
  • 3091

ios webservice+soap编程

ios 调用webservice已封装soap变量(基于ASIHTTPRequest类库),直接调用就好。
  • f10_s
  • f10_s
  • 2014-11-27 15:45
  • 2066

iOS下WebService接口调用与解析-一、Soap对象的封装

封装Soap对象Soap对象是对请求体的简单封装,免去重复而又容易出错的拼接工作。

如何用IOS调用WebService(SOAP接口)

方法一:使用WSDL2ObjC工具,将接口转成OC类。 1.在浏览器中输入webService接口地址(Safari不可用,我用的是Firefox),如:http://xxx.xxx.asmx,...

IOS之 soap协议 webservice篇

本次介绍一下ios使用soap协议和webs er

Webservice,Soap和Rest

由于第一次接触WebService,对于很多概念不太理解,尤其是看到各个OpenAPI的不同提供方式时,更加疑惑。如google map api采用了AJAX方式,通过javascript提供API,...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)