AFNetWorking3.1 出现code=-1016错误unacceptable content-type: application/xml"解决办法

本文详细介绍了在使用AF进行网络请求时遇到code=-1016错误的原因及解决方法。通过调整AF可接受的内容类型,成功解决了无法接收application/xml类型的问题。

昨天在测试接口时 出现code=-1016错误unacceptable content-type: application/xml"

Error Domain=com.alamofire.error.serialization.response Code=-1016 "Request failed: unacceptable content-type: application/xml" UserInfo={com.alamofire.serialization.response.error.response=<NSHTTPURLResponse: 0x608000223260>


在网上找了好久都没有这个问题的解决方法,然后花了一上午时间 解决了这个问题


问题描述:

在调试接口时,AF接收 的content type类型

manager.responseSerializer.acceptableContentTypes=[NSSetsetWithObjects:@"text/html",@"text/plain",@"text/xml", @"application/json",@"text/css",nil];

后台给的数据也是@"application/json"格式的,但是用AF请求后显示

显示的是问题是 无法接收content-type 为application/xml的类型

原因描述:

AF无法接收content-type 为application/xml的类型的数据。导致请求失败

需求描述:

在不影响后台大的改动情况下解决请求问题。

解决方案:

开始尝试在AF中添加content-type 为application/xml的类型

[manager.requestSerializer setValue:@"application/xml"forHTTPHeaderField:@"Content-Type"];

但是结果还是一样的。

虽然只是加了这个类型 但是后台那边还是json数据 所以还得加上这一句

[manager.requestSerializer setValue:@"application/json"forHTTPHeaderField:@"Accept"];

问题完美解决。

 


(base) michael_fang@DESKTOP-EJP73BE:~$ prefetch SAMEA5180620 prefetch SAMEA5180621 2025-09-07T15:52:13 prefetch.2.9.6 sys: connection failed while opening file within cryptographic module - mbedtls_ssl_handshake returned -9984 ( X509 - Certificate verification failed, e.g. CRL, CA or signature check failed ) 2025-09-07T15:52:13 prefetch.2.9.6 sys: mbedtls_ssl_get_verify_result returned 0x4008 ( !! The certificate is not correctly signed by the trusted CA !! The certificate is signed with an unacceptable hash. ) 2025-09-07T15:52:13 prefetch.2.9.6 sys: connection failed while opening file within cryptographic module - ktls_handshake failed while accessing &#39;130.14.29.110&#39; from &#39;172.24.99.168&#39; 2025-09-07T15:52:13 prefetch.2.9.6 sys: connection failed while opening file within cryptographic module - Failed to create TLS stream for &#39;www.ncbi.nlm.nih.gov&#39; (130.14.29.110) from &#39;172.24.99.168&#39; 2025-09-07T15:52:15 prefetch.2.9.6 sys: connection failed while opening file within cryptographic module - mbedtls_ssl_handshake returned -9984 ( X509 - Certificate verification failed, e.g. CRL, CA or signature check failed ) 2025-09-07T15:52:15 prefetch.2.9.6 sys: mbedtls_ssl_get_verify_result returned 0x4008 ( !! The certificate is not correctly signed by the trusted CA !! The certificate is signed with an unacceptable hash. ) 2025-09-07T15:52:15 prefetch.2.9.6 sys: connection failed while opening file within cryptographic module - ktls_handshake failed while accessing &#39;130.14.29.110&#39; from &#39;172.24.99.168&#39; 2025-09-07T15:52:15 prefetch.2.9.6 sys: connection failed while opening file within cryptographic module - Failed to create TLS stream for &#39;www.ncbi.nlm.nih.gov&#39; (130.14.29.110) from &#39;172.24.99.168&#39;
最新发布
09-08
<think> 嗯,用户的问题是关于prefetch工具2.9.6版本中出现的mbedtls_ssl_handshake返回-9984证书验证失败的解决办法。这个问题其实涉及到TLS连接的证书信任机制,尤其是prefetch依赖的mbedtls库如何验证服务器证书的有效性。 用户提供的引用日志很有价值,明确指出了两个关键点:一是证书没有被受信任的CA正确签名(X509 - Certificate verification failed),二是具体错误码0x8对应CA签名验证失败。这说明问题核心在于证书链的信任关系建立失败,而不是简单的过期或域名不匹配问题。 用户能准确提到工具版本和错误码,说明有一定技术背景,可能是个生物信息学领域的研究人员,正在使用prefetch下载NCBI的测序数据。这类用户通常需要快速解决问题以免耽误科研进度,所以解决方案必须清晰可操作。 首先想到三个主要解决方向:更新CA证书库、手动添加特定证书、或者跳过验证(虽然不推荐)。用户环境可能是Linux服务器,所以需要给出具体的证书管理命令。特别要注意prefetch 2.9.6使用mbedtls库,其证书路径配置方式可能与OpenSSL不同。 考虑到用户可能不熟悉mbedtls的配置,需要详细说明证书目录格式要求:必须每个CA证书单独.pem文件,且要求c_rehash生成的链接。对于紧急情况下的跳过验证方案,得强调安全风险,但也要给出具体环境变量设置方法,毕竟有些用户只是临时下载公开数据。 最后想到用户可能在机构防火墙后,所以补充了代理环境下的证书注意事项。那些wget/curl的测试命令很重要,既能帮助用户隔离问题,又能验证解决方案是否生效。 相关问题设计成递进式:从基础排查(时间同步)、到替代方案(aspera)、再到高级配置(自定义证书),这样用户可以根据自身情况选择深入程度。特别加入SRR示例是因为很多新手会卡在id格式问题,这其实和证书无关但经常被混淆。 </think> 根据prefetch工具在mbedtls证书验证失败(错误代码-9984)的典型解决方案,结合TLS连接特性,建议按优先级尝试以下方法: ### 一、更新证书信任链(推荐首选) 1. **更新CA证书包** ```bash # Ubuntu/Debian sudo apt update && sudo apt install ca-certificates # CentOS/RHEL sudo yum update ca-certificates ``` 2. **手动添加NCBI证书** • 下载Entrust根证书: ```bash wget https://secure.globalsign.com/cacert/Root-R1.crt -O /usr/local/share/ca-certificates/NCBI_Root.crt ``` • 更新证书库: ```bash sudo update-ca-certificates ``` ### 二、配置prefetch证书路径 在命令中显式指定证书路径(需确保证书文件存在): ```bash prefetch -X /etc/ssl/certs/ca-certificates.crt SRRXXXXXX ``` ### 三、临时绕过验证(仅限测试环境) ```bash export MBEDTLS_SSL_SKIP_VERIFY=1 # 跳过证书验证 prefetch SRRXXXXXX unset MBEDTLS_SSL_SKIP_VERIFY # 操作后立即禁用 ``` > **警告**:此操作存在中间人攻击风险,仅用于诊断问题 ### 四、网络环境特殊处理 若在**机构防火墙后**: 1. 联系IT部门获取企业根证书 2. 将证书放入`/usr/local/share/ca-certificates/` 3. 执行: ```bash sudo update-ca-certificates --fresh export SSL_CERT_DIR=/etc/ssl/certs # 显式指定路径 ``` ### 验证步骤 1. 测试证书有效性: ```bash openssl s_client -connect www.ncbi.nlm.nih.gov:443 -showcerts > ncbi.crt openssl verify -CAfile /etc/ssl/certs/ca-certificates.crt ncbi.crt ``` 2. 检查时间同步: ```bash date && ntpq -p # 确保证书验证时间有效 ``` > 若仍失败,可尝试下载新版证书包: > [cURL官方CA包](https://curl.se/docs/caextract.html) → 保存为`/etc/ssl/certs/cacert.pem` --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值