AFHTTPRequestOperation工作正常,但AFHTTPSessionManager不起作用

时间:2014-03-20 03:49:36

标签: ios soap wsdl afnetworking

我正在使用AFNetworking2.2.1对Web服务进行SOAP请求。我已经阅读了AFNetworking的文档,它说虽然AFHTTPRequestOperationManager通常是提出请求的最佳方式,但{{1}可以单独使用。这意味着推荐的方式是使用AFHTTPRequestOperation。首先,我使用AFHTTPSessionManager,但它不起作用。然后我尝试AFHTTPSessionManager,它工作正常。这让我很困惑。

以下是两个代码快照。使用AFHTTPRequestOperation时不起作用,而AFHTTPSessionManager则可以正常工作。

我的问题是为什么AFHTTPSessionManager不起作用以及如何更改它。

AFHTTPRequestOperation

这是使用[[SLAppNetAPIClient sharedClient] POST:@"webservices/wsLogin.php?wsdl" parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) { NSString *soapMessage = [NSString stringWithFormat: @"<?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" "<DGLogInWork xmlns=\"http://10.0.0.22:3301/\">\n" "<szHostName>%@</szHostName>\n" "<szLoginMAC>%@</szLoginMAC>\n" "<szLoginIP>%@</szLoginIP>\n" "</DGLogInWork>\n" "</soap:Body>\n" "</soap:Envelope>\n", [SLDevice name], [SLDevice deviceMAC], [SLDevice deviceIP]]; debugLog(@"soapMessage:%@", soapMessage); NSDictionary *header = @{@"Content-Type": @"text/xml"}; NSData *body = [soapMessage dataUsingEncoding:NSUTF8StringEncoding]; [formData appendPartWithHeaders:header body:body]; } success:^(NSURLSessionDataTask *task, id responseObject) { debugLog(@"responseObject~~~: %@", responseObject); } failure:^(NSURLSessionDataTask *task, NSError *error) { debugLog(@"error :%@", error); }]; AFHTTPSessionManagerSLAppNetAPIClient的子类。

AFHTTPSessionManager

这是使用NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://10.0.0.22:3301/webservices/wsLogin.php?wsdl"]]; [request setValue:@"text/xml" forHTTPHeaderField:@"Content-type"]; [request setHTTPMethod:@"POST"]; NSString *soapMessage = [NSString stringWithFormat: @"<?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" "<DGLogInWork xmlns=\"http://10.0.0.22:3301/\">\n" "<szHostName>%@</szHostName>\n" "<szLoginMAC>%@</szLoginMAC>\n" "<szLoginIP>%@</szLoginIP>\n" "</DGLogInWork>\n" "</soap:Body>\n" "</soap:Envelope>\n", [SLDevice name], [SLDevice deviceMAC], [SLDevice deviceIP]]; [request setHTTPBody:[soapMessage dataUsingEncoding:NSUTF8StringEncoding]]; AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request]; [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) { debugLog(@"responseObject = %@", [operation responseString]); } failure:^(AFHTTPRequestOperation *operation, NSError *error) { debugLog(@"error = %@", error); }]; [operation start];

我不知道他们之间有什么区别。希望有人可以提供帮助。非常感谢。

0 个答案:

没有答案