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.

up vote
1
down vote

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 = @"\r\n";
    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;
}

时间: 2024-12-20 01:20:53

AFHTTPRequestOperationManager的post方法的相关文章

AFNetworking 使用  基础篇

1. AFN一套网络操作的第三方框架a. NSURLConnection iOS2.0之后就有b. NSURLSession iOS7.0之后才有c. 默认支持序列化和反序列化json,xml需要自己解析 d. 优秀的错误处理机制e. 封装了Reachability f. 支持https AFHTTPRequestOperationManager初始化的方法 ○ self.requestSerializer = [AFHTTPRequestSerializer serializer];请求数据的

iOS网络编程 AFNetwording框架的解析与使用

AFN介绍 AFN是一套操作网络的第三方框架 NSURLConnection   iOS2.0之后出现 NSURLSession         iOS7.0之后出现 默认支持序列化和反序列化json,xml数据需要自己解析 是一套优秀的错误处理机制 封装了Reachability 支持HTTPS(iOS9.0默认都是https网络协议,但可以在into.pilst中修改属性为所有协议都可以) GET演示 url是字符串,如果有汉字和空格需要url编码 获取的数据直接进行了反序列化(如果获取的不

这个方法可以留着 + 上传图片

上传图片: 首先导入 afnemworking 这个库 AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"]; NSDictionary *parameters [email protected]{@&qu

AFNetworking 使用方法(2.0)

AFNetworking 使用方法(2.0) 分类: IOS2014-11-12 09:17 2018人阅读 评论(0) 收藏 举报 目录(?)[+] 本文介绍的是AFNetworking-2.0 使用方法(增加适应:不完善的head內的 meta的content格式) 随着asihttprequest的停止更新,许多人都转向了AFNetworking. MKNetworkKit.我也是其中一个.于是我从网上找了许多文章作参考,但是结果都是失败告终.研究了好久都搞不透,最后还是请人帮忙搞定了.经

上传图片流到服务器(AFN方法) (多张图片)(图片流)

上传图片流到服务器(AFN方法) (多张图片)(图片流) 第一步//获取图片 UIAlertController *actionSheet = [UIAlertController alertControllerWithTitle:@"添加照片" message:nil preferredStyle:UIAlertControllerStyleActionSheet]; UIAlertAction *cancel = [UIAlertAction actionWithTitle:@&q

开发经验: 对AFN和ASI各自使用方法及区别的总结

经过多年的iOS开发, 现总结一下HTTP协议下的两大网络处理第三方框架的使用心得 首先来说下AFNetworking: 一.2大管理对象1.AFHTTPRequestOperationManager* 对NSURLConnection的封装 2.AFHTTPSessionManager* 对NSURLSession的封装 二.AFHTTPRequestOperationManager的具体使用1.创建管理者AFHTTPRequestOperationManager \*mgr = [AFHTT

iOS—修改AFNetworking源文件可接收text/plain的方法

iOS—修改AFNetworking源文件可接收text/plain的方法 在使用AFNetworking的时候可能会碰到下面的错误: { status code: 200, headers { "Content-Length" = 14; "Content-Type" = "text/plain;charset=utf-8"; Date = "Thu, 22 May 2014 10:37:50 GMT"; Server =

iOS网络第三方使用方法大全AFNetworking/ASIHTTPRequest/MKNetworkKit

1. 参与对比的孩子有 AFNetworking/ASIHTTPRequest/MKNetworkKit 1. GET 1~ 新建工程: SingleProject 带故事板的. 为了我们测试的方便 2~ 打开系统命令提示符 ->  使用cocoapods 下载这3个第三方库 -> 如果不会使用cocoapods请百度cocoapods-> 然后确认环境. 进行安装 3~ podfile: platform: iOS, '7.0' pod "AFNetworking"

AFNetworking 的请求方法

AFNetworking 使用方法(2.0) 本文介绍的是AFNetworking-2.0 使用方法(增加适应:不完善的head內的 meta的content格式) 随着asihttprequest的停止更新,许多人都转向了AFNetworking. MKNetworkKit.我也是其中一个.于是我从网上找了许多文章作参考,但是结果都是失败告终.研究了好久都搞不透,最后还是请人帮忙搞定了.经常从网上索取免费资料的一员,要有回报的思想,也为了让更多的人少走些弯路,所以下面是代码:(有错误可以指出)