zoukankan      html  css  js  c++  java
  • AFHTTPRequestOperationManager的post方法

    使用afnetworking2.0下,AFHTTPRequestOperationManager的post方法向服务器发送用户名和密码,参数名都正确且都已经赋值,为什么服务端接收到得数据是空的

    + (AFHTTPRequestOperationManager *)httpRequestOperationManager
    {
        NSURL *baseUrl = [NSURL URLWithString:@"http://192.168.1.234:8081"];
    
        AFHTTPRequestOperationManager *httpRequestOperationManager = [[AFHTTPRequestOperationManager alloc] initWithBaseURL:baseUrl];
    
        //httpRequestOperationManager.requestSerializer = [AFJSONRequestSerializer serializer];之前这里没有注释掉,服务端接收不到post过去的参数,注释掉之后正常,目前还不清楚是什么原因
    
        httpRequestOperationManager.responseSerializer = [AFJSONResponseSerializer serializer];
    
        httpRequestOperationManager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
    
        // 检测网络情况
        [httpRequestOperationManager.reachabilityManager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
    
            switch (status) {
    
                case AFNetworkReachabilityStatusReachableViaWWAN:
                    NSLog(@"当前网络可用");
                    break;
                case AFNetworkReachabilityStatusReachableViaWiFi:
                    NSLog(@"当前网络可用");
                    break;
                case AFNetworkReachabilityStatusNotReachable:
                    NSLog(@"当前网络不可用");
                    break;
                default:
                    break;
            }
        }];
    
        // 开启检测
        [httpRequestOperationManager.reachabilityManager startMonitoring];
    
        return httpRequestOperationManager;
    }

    StackOverflow 封装方法

     
       

    The server is sending back a response with status code 500, which means that your server encountered an error while attempting to process the request. There doesn't appear to be anything wrong with how you're using AFNetworking, but the only way to tell is to debug things on the server side first.

    It has been a while since I solved this but maybe this could still help someone. Eventually, in my case, the upper level httpPOSTMultiPartRequestWithPath method did not cut and I needed more flexibility in the message structure (for instance, setting custom boundaries). I ended up using the HTTPRequestOperationWithRequest method and created the URLrequest manually.

    -(void)httpPOSTmultipartContentWithPath:(NSString*)path Parameters:(NSDictionary*)parameters Body:(NSData*)body Completion:(APICompletionBlock)apiComp{
    
        NSURL *pathURL = [NSURL URLWithString:path];
        NSURLRequest *request = [self POSTRequestWithURL:pathURL DataDictionary:parameters andBody:body];
    
        AFHTTPRequestOperation *operation = [self HTTPRequestOperationWithRequest:request
                                                                          success:^(AFHTTPRequestOperation *operation, id responseObject) {
                                                                              apiComp(responseObject,nil);
                                                                          } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                                                                              apiComp(nil,error);
                                                                          }];
    
        [operation start];
    }
    
    - (NSMutableURLRequest *)POSTRequestWithURL:(NSURL *)url DataDictionary:(NSDictionary *)dictionary andBody:(NSData*)body
    {
    
        // Create a POST request
        NSMutableURLRequest *myMedRequest = [NSMutableURLRequest requestWithURL:url];
        [myMedRequest setHTTPMethod:@"POST"];
    
        // Add HTTP header info
    
        NSString *boundary = @"*****";
        NSString *lineEnd = @"
    ";
        NSString *twoHyphens = @"--";
        [myMedRequest addValue:[NSString stringWithFormat:@"multipart/form-data;boundary= %@", boundary] forHTTPHeaderField:@"Content-Type"];
    
        //create HTTP Body
        NSMutableData *POSTBody = [NSMutableData data];
        [POSTBody appendData:[[NSString stringWithFormat:@"boundary=%@%@%@",twoHyphens,boundary,lineEnd] dataUsingEncoding:NSUTF8StringEncoding]];
        [POSTBody appendData:[[NSString stringWithFormat:@"Content-Type:multipart/related;type=application/json;boundary=%@%@%@%@",twoHyphens,twoHyphens,boundary,lineEnd] dataUsingEncoding:NSUTF8StringEncoding]];
        [POSTBody appendData:[[NSString stringWithFormat:@"Content-Id:jsonTemp%@",lineEnd] dataUsingEncoding:NSUTF8StringEncoding]];
        [POSTBody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name="json"%@%@", lineEnd,lineEnd] dataUsingEncoding:NSUTF8StringEncoding]];
        //add JSON dictionary with request inforamtion
        [POSTBody appendData:[[NSString stringWithFormat:@"%@%@",dictionary
         ,lineEnd] dataUsingEncoding:NSUTF8StringEncoding]];
    
        [POSTBody appendData:[lineEnd dataUsingEncoding:NSUTF8StringEncoding]];
        [POSTBody appendData:[[NSString stringWithFormat:@"%@%@%@",twoHyphens,boundary,lineEnd] dataUsingEncoding:NSUTF8StringEncoding]];
        [POSTBody appendData:[[NSString stringWithFormat:@"Content-Type:image/jpg"] dataUsingEncoding:NSUTF8StringEncoding]];
        [POSTBody appendData:[[NSString stringWithFormat:@"%@",lineEnd] dataUsingEncoding:NSUTF8StringEncoding]];
        [POSTBody appendData:[[NSString stringWithFormat:@"Content-Id:%@",@"profile.jpg"] dataUsingEncoding:NSUTF8StringEncoding]];
        [POSTBody appendData:[[NSString stringWithFormat:@"%@",lineEnd] dataUsingEncoding:NSUTF8StringEncoding]];
        [POSTBody appendData:[[NSString stringWithFormat:@"%@",lineEnd] dataUsingEncoding:NSUTF8StringEncoding]];
    
        // Add image data
        [POSTBody appendData:body];
    
    
        // Add the closing -- to the POST Form
        [POSTBody appendData:[[NSString stringWithFormat:@"%@",lineEnd] dataUsingEncoding:NSUTF8StringEncoding]];
        [POSTBody appendData:[[NSString stringWithFormat:@"%@%@%@%@",twoHyphens,boundary,twoHyphens, lineEnd] dataUsingEncoding:NSUTF8StringEncoding]];
    
    
        // Add the body to the myMedRequest & return
        [myMedRequest setHTTPBody:POSTBody];
        return myMedRequest;
    }
  • 相关阅读:
    Sonar系列:IDEA集成SonarLint(二)
    Jenkins 安装部署全过程
    使用Docker搭建Sonarqube
    Sonar系列:SonarQube+SonarScanner 最全安装步骤(一)
    微信小程序反编译解包教程
    一张图告诉你,如何攻击Java Web应用
    windows server2019共享文件配额报错The quota limit is 10240.00 MB and the current usage is 10213.39 MB (99% of limit)的处理
    jenkins使用ssh remote插件执行shell后无法退出的问题处理
    基于docker镜像centos:7 镜像制作自定义的centos及tomcat/php/nginx镜像
    centos7.9环境基于docker和pipeline构建jenkins的ci平台
  • 原文地址:https://www.cnblogs.com/allanliu/p/4225495.html
Copyright © 2011-2022 走看看