AFHTTPRequestOperation works fine but AFHTTPSessionManager doesn't work

喜欢而已 提交于 2020-03-24 14:25:17

问题


I am doing an SOAP request with a web service using AFNetworking2.2.1 .I have read the documentation on AFNetworking, it says that Although AFHTTPRequestOperationManager is usually the best way to go about making requests, AFHTTPRequestOperation can be used by itself.It means that the recommended way is using the AFHTTPSessionManager.First, I use the AFHTTPSessionManager,but it doesn't work.Then I tried AFHTTPRequestOperation,it works fine.This really confused me.

Here are the two code snapshots.It doesn't work when using AFHTTPSessionManager,while it works fine with AFHTTPRequestOperation.

My question is why AFHTTPSessionManager doesn't work and how to change it.

[[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);
}];

This is using the AFHTTPSessionManager.SLAppNetAPIClient is a subclass of 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];

This is using AFHTTPRequestOperation.

I don't know what the difference between them.Hope someone could help.Thanks a lot.

来源:https://stackoverflow.com/questions/22523091/afhttprequestoperation-works-fine-but-afhttpsessionmanager-doesnt-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!