AFNetworking Post 出错 Code=-1011 Request failed: bad request (400)

今天使用新版AFNetWorking(3.0 beta3) 竟然出错了,错误信息如下:

Error Domain=com.alamofire.error.serialization.response Code=-1011 "Request failed: bad request (400)" UserInfo=0x7fbf4947edf0 {com.alamofire.serialization.response.error.response=...

遇到这种错误不要慌,先看看你post时带的参数params是不是有问题,是服务器不认识的格式?或者是...你字段写错了!仔细检查下你的字段是不是写错了...

Stack Overflow上有位大神这样说:

The error says it all: you got a 400 response from the server, meaning that what you sent was either not formatted properly, or the server just couldn't understand it.

还不行的话,请与你们后台联系..

你可以这样查看返回的中文错误信息:

NSData *data = error.userInfo[@"com.alamofire.serialization.response.error.data"] ;
NSString *errorStr = [[ NSString alloc ] initWithData:data encoding:NSUTF8StringEncoding];


/**
 *  根据code获得一个accessToken(发送一个POST请求)
 *
 *  @param code 授权成功后的请求标记
 */
- (void)accessTokenWithCode:(NSString *)code
{
    // 1.获得请求管理者
    AFHTTPSessionManager *mgr = [AFHTTPSessionManager manager];
    
    // 2.封装请求参数
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"client_id"] = JXAppKey;
    params[@"client_secret"] = JXAppSecret;
    params[@"redirect_uri"] = JXRedirectURI;
    params[@"grant_type"] = @"authorization_code";
    params[@"code"] = code;

    // 3.发送post请求
    [mgr POST:@"https://api.weibo.com/oauth2/access_token" parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, NSDictionary *accountDict) {
        // 隐藏HUD
        [MBProgressHUD hideHUD];
        
        JXLog(@"请求成功--%@", accountDict);
        
        // 字典转成模型
        JXAccount *account = [JXAccount accountWithDict:accountDict];
        
        // 存储账号模型
        [JXAccountTool saveAccount:account];
        
        // 切换控制器(可能去新特性\tabbar)
        [[UIApplication sharedApplication].keyWindow switchRootViewController];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        // 隐藏HUD
        [MBProgressHUD hideHUD];
        
        JXLog(@"请求失败--%@", error);
    }];


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值