AFNetworking 返回错误unsupported med

2018-03-28  本文已影响24人  骑马纵天下

一.修改Content-Type数据类型

一般情况下,AFNetwoking的默认Content-Type是application/x-www-form-urlencodem。如果服务器要求的格式是JSON,我们需要把Content-Type设置为applicaiton/json。

服务器是通过识别Content-Type来识别传送的数据类型,分辨传送的数据是文本,图片或者是其他。如果服务器不识别对应的Content-Type,那么就会返回unsupported media type (415).

修改Content-Type代码如下:
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.requestSerializer = [AFJSONRequestSerializer serializer]; 
[manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
二.AFN请求头添加公共参数
NSString *codestr=[@"123456" MD5];

NSString *parameters = [@{@"username":@"18658114668",@"password":codestr} mj_JSONString];

NSString *loginURL = @"[http://172.16.150.144:8090/nursing-branch-web/auth/user/mobilelogin](http://172.16.150.144:8090/nursing-branch-web/auth/user/mobilelogin)";

AFHTTPRequestOperationManager *manager  = [AFHTTPRequestOperationManager manager];

manager.requestSerializer = [AFJSONRequestSerializer serializer];

manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html",@"text/plain", nil];//申明返回的结果是json类型

[manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];//申明请求类型

//请求头中添加的公共参数

[manager.requestSerializer setValue:@"1" forHTTPHeaderField:@"app-api-version"];

[manager.requestSerializer setValue:@"6624effa7d352c0a807a9197b9b" forHTTPHeaderField:@"pri-token"];

[manager.requestSerializer setValue:@"2ac87cfc9088a1530652d0e83c" forHTTPHeaderField:@"pri-uuid"];

[manager.requestSerializer setValue:@"-PEnQxvWspHp-gWg" forHTTPHeaderField:@"token"];

[manager POST:loginURL parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {

      HULog(@"成功");

} failure:^(AFHTTPRequestOperation *operation, NSError *error) {

      HULog(@"错误");

    }];
上一篇下一篇

猜你喜欢

热点阅读