iOS 报错"NSErrorFailingURLKey"的解决

错误打印如下

1 Error: Error Domain=com.alamofire.error.serialization.response Code=-1016 "Request failed: unacceptable content-type: text/plain" UserInfo={com.alamofire.serialization.response.error.response=<NSHTTPURLResponse: 0x17423e700> { URL: http://114.112.104.149:20012/app/consult/create/quick } { status code: 200, headers {
2 "Content-Length" = 188;
3 Date = "Mon, 06 Mar 2017 07:16:35 GMT";
4 Expires = "Thu, 01 Jan 1970 00:00:00 GMT";
5 Server = "Jetty(9.3.10.v20160621)";
6 "Set-Cookie" = "JSESSIONID=1m6o3zh51yh5n131yev2ezd29h;Path=/app;HttpOnly";
7 } }, NSErrorFailingURLKey=http://114.112.104.149:20012/app/consult/create/quick, com.alamofire.serialization.response.error.data=<7b227479 7065223a 3131322c 22706179 4c6f6164 223a7b22 61757468 656e7469 63617465 55726c22 3a226874 74703a2f 2f313134 2e313132 2e313034 2e313439 3a323030 32302f63 61732f61 70704c6f 67696e3f 73657276 6963653d 30303030 30313561 38653162 64623265 2675726c 3d614852 3063446f 764c7a45 784e4334 784d5449 754d5441 304c6a45 304f546f 794d4441 784d6939

刚开始是认为AFN不支持"text/plain"样式, 但是自己在单例中已补充了这个类型, 补充的如下:

1  manager.responseSerializer.acceptableContentTypes =[NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript", @"text/html", @"text/plain", @"multipart/form-data", @"image/jpeg", @"image/png", @"application/octet-stream", nil];

不是这个的原因.

但是, 在服务器未认证之前会有这个问题(我们公司的项目要实时认证才能访问服务器), 认证之后就不存在这个问题了, 那一定是认证的时候做了些什么事情, 所以我就去看认证的时候干了些什么, 结果我发现再认证的时候我加了这几句代码:

1 //增加这几行代码;
2     AFSecurityPolicy *securityPolicy = [[AFSecurityPolicy alloc] init];
3     [securityPolicy setAllowInvalidCertificates:YES];
4     [manager setSecurityPolicy:securityPolicy];
5     manager.responseSerializer = [AFHTTPResponseSerializer serializer];

就不在报这个错误论了, OK!

要加上安全策略.

 

posted on 2017-03-09 21:27  loveDoDream_zzt  阅读(1745)  评论(0编辑  收藏  举报

导航