使用AFNetworking遇到异常 Request failed: unacceptable content-type: text/html


转自:http://www.jianshu.com/p/4bfcae659a44

iOS 使用AFNetworking遇到错误 Request failed: unacceptable content-type: text/html
错误日志:
Error Domain=com.alamofire.error.serialization.response Code=-1016 "Request failed: unacceptable content-type: text/html" UserInfo={com.alamofire.serialization.response.error.response=<NSHTTPURLResponse: 0x7f999be478d0> { URL: myUrlXXXXXX } { status code: 200, headers {
"Cache-Control" = "no-store, no-cache, must-revalidate, post-check=0, pre-check=0";
Connection = "keep-alive";
"Content-Encoding" = gzip;
"Content-Type" = "text/html; charset=utf-8";
Date = "Tue, 24 Nov 2015 09:06:04 GMT";
Expires = "Thu, 19 Nov 1981 08:52:00 GMT";
Pragma = "no-cache";
Server = "nginx/1.1.19";
"Set-Cookie" = "PHPSESSID=qdvtek1k91oeva2u8fats39l93; path=/";
"Transfer-Encoding" = Identity;
"X-Powered-By" = "PHP/5.3.10-1ubuntu3.21";
} }, NSErrorFailingURLKey=http:URL: myUrlXXXXXX, com.alamofire.serialization.response.error.data=<7b226572 726f725f 636f6465 223a302c 22657272 6f725f6d 7367223a 22222c22 75706c6f 61645f75 726c223a 22687474 703a5c2f 5c2f7777 772e7467 7370792e 636f6d5c 2f6c6f6f 70657273 5c2f7365 72766572 5c2f6269 6e5c2f63 7573746f 6d65725c 2f75706c 6f61645c 2f373332 31746f75 7869616e 672e6a70 67227d>, NSLocalizedDescription=Request failed: unacceptable content-type: text/html}


原因:
不可接受的内容类型 “text/html
解决方案:
AFJSONResponseSerializer.m中,222行左右
把 这句: self.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript", nil];
修改为:
self.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html", nil];

在进行网络请求时出现-1016 是因为只支持
text/json,application/json,text/javascript
你可以添加text/html
一劳永逸的方法是 在
AFURLResponseSerialization.h
里面搜索
self.acceptableContentTypes
然后 在里面 添加
@"text/html",@"text/plain"
这样就可以解决-1016的错误了
但是随之而来的是3840错误
Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 3840.)" (JSON text did not start with array or object and option to allow fragments not set.) UserInfo=0x9152780 {NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set.}
你会发现出现此错误
怎么办呢
添加如下语句 就可以解决问题了
manger.requestSerializer= [AFHTTPRequestSerializerserializer];


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
javax.net.ssl.SSLHandshakeException: Unacceptable certificate错误是由于访问的域名证书不在有效期内或者JDK中不存在该证书的信任导致的。解决这个问题的方法有以下几种: 1. 更新JDK信任库:可以通过更新JDK的信任库来解决该问题。可以使用以下命令将证书添加到信任库中: ```shell keytool -import -alias <alias> -keystore <path_to_truststore> -file <path_to_certificate> ``` 其中,`<alias>`是证书的别名,`<path_to_truststore>`是信任库的路径,`<path_to_certificate>`是证书的路径。 2. 忽略证书验证:在某些情况下,可以选择忽略证书验证来解决该问题。可以通过以下代码来实现: ```java TrustManager[] trustAllCerts = new TrustManager[]{ new X509TrustManager() { public java.security.cert.X509Certificate[] getAcceptedIssuers() { return null; } public void checkClientTrusted( java.security.cert.X509Certificate[] certs, String authType) { } public void checkServerTrusted( java.security.cert.X509Certificate[] certs, String authType) { } } }; SSLContext sc = SSLContext.getInstance("TLS"); sc.init(null, trustAllCerts, new java.security.SecureRandom()); HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory()); ``` 3. 添加自定义信任证书:如果访问的是自签名证书或者不受信任的证书,可以将该证书添加到信任库中。可以使用以下命令将证书添加到信任库中: ```shell keytool -import -alias <alias> -keystore <path_to_truststore> -file <path_to_certificate> ``` 其中,`<alias>`是证书的别名,`<path_to_truststore>`是信任库的路径,`<path_to_certificate>`是证书的路径。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值