AFNetworking POST 请求参数保存在Body 中的解决办法

时间:2015-08-12 18:54:12   收藏:0   阅读:9888

1)首先区分一下,get和post的区别

get方法,会将参数放到url中,属于透传,相对于post放到content中的安全性低

2)使用AFNetworking,默认HTTPMethodsEncodingParametersInURI里面包含的只有`GET`, `HEAD`, 和 `DELETE` .不知道情况的情况下使用POST方法的话,会将用户传递的参数放到Body里面,导致服务端JSP通过request.getParameters()获取不到参数。

3)通过xcode定位代码,发现在requestBySerializingRequest中会直接跳过

 if ([self.HTTPMethodsEncodingParametersInURI containsObject:[[request HTTPMethod] uppercaseString]]) {
        return [super requestBySerializingRequest:request withParameters:parameters error:error];
   }

直接走下面的步骤,然后将请求参数添加到body里面:

[mutableRequest setHTTPBody:[NSJSONSerialization dataWithJSONObject:parameters options:self.writingOptions error:error]];

4)解决办法:

HTTPMethodsEncodingParametersInURI 包含POST方法

requestManager.requestSerializer.HTTPMethodsEncodingParametersInURI = [NSSet setWithArray:@[@"POST", @"GET", @"HEAD"]];//一般的服务器不推荐使用put和delete,所以这里就没有添加

原文:http://www.cnblogs.com/allen2015/p/4724931.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!