2

我在 Stackoverflow 中查看了几篇文章(thisthis),但没有一篇回答“如何使用AFHTTPSessionManager基于 SOAP 的 Web 服务”。

这是不可能的,还是我们需要做一些调整,比如子类化等?

我可以使用基于 SOAP 的调用,AFHTTPRequestOperation但这是我不想使用的,因为我在那里使用 XML 和 JSON Web 服务AFHTTPSessionManager。我希望在整个应用程序中采用类似的方法。

我使用的代码RequestOperation是:

NSString *soapMessage = @"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
"<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">n"
"<soap:Body>\n"
"<CelsiusToFahrenheit xmlns=\"http://tempuri.org/\">\n"
"<Celsius>50</Celsius>\n"
"</CelsiusToFahrenheit>\n"
"</soap:Body>\n"
"</soap:Envelope>n";

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://tempuri.org/"]];
[request setValue:@"text/xml" forHTTPHeaderField:@"Content-type"];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:[soapMessage dataUsingEncoding:NSUTF8StringEncoding]];

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

[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSLog(@"responseObject = %@", [operation responseString]);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"error = %@", error);
}];

[operation start];

我尝试使用SessionManager,但这不起作用:(request与上面的代码片段相同),我得到dataasnil并且response有一些值,其中也error有一些值。

AFHTTPSessionManager *sManager = [[AFHTTPSessionManager alloc] init];

[sManager dataTaskWithRequest:(NSURLRequest *)request
            completionHandler:^( NSURLResponse *response, NSData *data, NSError *error){

                NSLog(@"Data: %@", data);
                NSLog(@"Resp: %@", response);
                NSLog(@"Err: %@", error);
}];
4

1 回答 1

1

我尝试了几个小时,终于能够得到一些响应并且没有错误。这是更新的代码:

NSString *soapMessage = @"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
"<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">n"
"<soap:Body>\n"
"<CelsiusToFahrenheit xmlns=\"http://tempuri.org/\">\n"
"<Celsius>50</Celsius>\n"
"</CelsiusToFahrenheit>\n"
"</soap:Body>\n"
"</soap:Envelope>n";

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://tempuri.org/"]];
[request setValue:@"text/xml" forHTTPHeaderField:@"Content-type"];
[request setHTTPMethod:@"POST"];

[request setHTTPBody:[soapMessage dataUsingEncoding:NSUTF8StringEncoding]];


AFHTTPSessionManager *sManager = [[AFHTTPSessionManager alloc] init];
sManager.responseSerializer = [AFHTTPResponseSerializer serializer];

NSURLSessionDataTask *dataTask = [sManager dataTaskWithRequest:(NSURLRequest *)request
            completionHandler:^( NSURLResponse *response, id responseObject, NSError *error){
                NSString *resString = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
                NSLog(@"Data: %@", resString);
                //NSLog(@"Resp: %@", [response ]);
                NSLog(@"Err: %@", error);
            }];
[dataTask resume];
于 2015-10-28T14:29:00.820 回答