iOS"Request failed: unacceptable content-type: text/html"

本文解决了使用AFNetworking时遇到的服务器返回不可接受的内容类型text/html的问题。通过修改AFURLResponseSerialization.m文件中acceptableContentTypes设置,添加text/html为可接受的内容类型。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

以前用的好端端的接口,今天访问居然出错了,但是再用浏览器测试,发现可以正常返回数据,甚是奇怪啊。

下面是错误信息:

获取服务器响应出错 error=Error Domain=com.alamofire.error.serialization.response Code=-1016 "Request failed: unacceptable content-type: text/html" UserInfo=0x7fdfd8729680 {com.alamofire.serialization.response.error.response= { URL: http://172.16.1.31:7001/itom/getwork } { status code: 200, headers {

    "Cache-Control" = "no-cache";

    "Content-Type" = "text/html;charset=UTF-8";

    Date = "Mon, 24 Nov 2014 03:13:16 GMT";

    "Transfer-Encoding" = Identity;

    "X-Powered-By" = "Servlet/2.5 JSP/2.1";

} }, NSErrorFailingURLKey=http://172.16.1.31:7001/itom/getwork, com.alamofire.serialization.response.error.data=<5b7b2273 74617475 73223a22 73756363 65737322 2c226d73 67223a22 e799bbe9 9986e688 90e58a9f 227d5d>, NSLocalizedDescription=Request failed: unacceptable content-type: text/html}


下面是百度出来的答案:

I also encountered the same problem. This means that your server is sending "text/html" instead of the already supported types. After a little search, my solution was to add "text/html" to acceptableContentTypes set in AFURLResponseSerialization class. Just search for "acceptableContentTypes" and add @"text/html" to the set manually. Of course, the ideal solution will be to change the tpe from the server, but for that you will hade to talk with the server team.

I hope this helps you. Best regards and less bugs as possible in the code.

 op.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html”];


对应到自己的项目里面,我用的是AFNetworking这套网络请求包,需要改的是:

AFURLResponseSerialization.m文件

223行:

self.acceptableContentTypes = [NSSetsetWithObjects:@"application/json", @"text/html",@"text/json",@"text/javascript", nil];

加上蓝色部分,其实就是添加一种服务器返回的数据格式。

<think>我们正在处理一个关于MQTT连接的错误:"Connection refused: Unacceptable protocol version"。这个错误通常发生在MQTT客户端尝试连接服务器时,客户端和服务器在协议版本上无法达成一致。 ### 错误原因分析 MQTT协议有多个版本,最常见的是MQTT 3.1、3.1.1和5.0。当客户端尝试使用服务器不支持的协议版本进行连接时,服务器会拒绝连接并返回此错误。 ### 解决步骤 #### 1. 确认服务器支持的协议版本 首先,需要确定MQTT服务器支持哪些协议版本。这通常可以在服务器的配置文件中找到,或者查阅服务器文档。例如,Mosquitto服务器默认支持MQTT 3.1.1和5.0,但可能禁用了旧版本。 #### 2. 检查客户端使用的协议版本 然后,检查客户端代码或配置中指定的协议版本。例如,在Python中使用`paho-mqtt`客户端时,创建连接时会指定协议版本: ```python import paho.mqtt.client as mqtt client = mqtt.Client(protocol=mqtt.MQTTv311) # 指定使用MQTT v3.1.1 ``` 常见的协议版本常量有: - `mqtt.MQTTv31`:MQTT 3.1 - `mqtt.MQTTv311`:MQTT 3.1.1 - `mqtt.MQTTv5`:MQTT 5.0 #### 3. 调整客户端协议版本 如果客户端使用的协议版本不被服务器支持,需要调整客户端以使用服务器支持的版本。例如,如果服务器只支持MQTT 3.1.1,而客户端使用MQTT 3.1,则应将客户端改为使用3.1.1。 #### 4. 服务器配置调整(可选) 如果可能,也可以调整服务器配置以支持更多协议版本。例如,在Mosquitto中,可以通过配置文件启用对MQTT 3.1的支持(尽管不推荐,因为3.1是较旧的版本)。 #### 5. 使用协议版本协商(MQTT 5.0) 如果客户端和服务器都支持MQTT 5.0,则可以使用该版本的协议,它提供了更好的特性。但在连接时,客户端可以尝试使用5.0,如果服务器不支持,可以降级(但通常客户端库不会自动降级,需要手动处理)。 #### 6. 测试连接 修改后,重新测试连接。 ### 示例代码(Python) 假设我们使用`paho-mqtt`,并希望尝试多个协议版本直到成功: ```python import paho.mqtt.client as mqtt def connect_with_fallback(host, port=1883): # 按优先级尝试的协议版本列表 protocols = [mqtt.MQTTv5, mqtt.MQTTv311, mqtt.MQTTv31] for protocol in protocols: client = mqtt.Client(protocol=protocol) try: client.connect(host, port) print(f"Connected with protocol version: {protocol}") return client except Exception as e: print(f"Failed with protocol {protocol}: {str(e)}") return None client = connect_with_fallback("mqtt.eclipseprojects.io") if client: client.loop_start() else: print("All protocol versions failed") ``` ### 注意事项 - 一些服务器可能只支持一个协议版本(如只支持3.1.1或5.0),因此需要明确知道服务器支持的版本。 - 如果服务器要求加密连接(SSL/TLS),则连接时需要配置证书等。 ### 引用说明 在MQTT协议中,CONNECT消息包含协议版本号,如果服务器不接受该版本,会返回CONNACK报文,其中返回码为0x01(表示不支持的协议版本)[^1]。 [^1]: MQTT协议规范中规定,当服务器不支持客户端请求的协议级别时,返回连接确认(CONNACK)报文,返回码为1(Unacceptable protocol version)。 §§相关问题§§ 1. MQTT协议有哪些版本?它们之间有什么区别? 2. 如何配置Mosquitto服务器以支持不同的MQTT协议版本? 3. 除了协议版本不匹配,还有哪些原因会导致MQTT连接被拒绝?</think>### 解决 "Connection refused: Unacceptable protocol version" 错误 该错误发生在 MQTT 客户端与服务器通信时,双方使用的协议版本不兼容。以下是系统化的解决步骤: #### 1. **确认服务器支持的协议版本** - 检查 MQTT 服务器配置(如 Mosquitto 的 `mosquitto.conf`): ```bash # 查看支持的协议版本 listener 1883 protocol mqtt # 支持 MQTT 3.1/3.1.1 ``` - 或通过管理命令查看(如 EMQX): ```bash emqx_ctl listeners list ``` #### 2. **调整客户端协议版本** - **Python 示例(Paho-MQTT)**: ```python import paho.mqtt.client as mqtt # 强制使用 MQTT v3.1.1(兼容性最好) client = mqtt.Client(mqtt.CallbackAPIVersion.VERSION2, protocol=mqtt.MQTTv311) client.connect("broker.example.com", 1883) ``` - **Java 示例(Eclipse Paho)**: ```java MqttClient client = new MqttClient("tcp://broker.example.com:1883", "clientId"); MqttConnectOptions options = new MqttConnectOptions(); options.setMqttVersion(MqttConnectOptions.MQTT_VERSION_3_1_1); // 显式指定版本 client.connect(options); ``` #### 3. **检查服务器协议限制** 某些服务器默认禁用旧版本: - **Mosquitto 配置**(添加至 `mosquitto.conf`): ```bash # 启用 MQTT v3.1 支持 listener 1883 protocol mqtt allow_legacy_protocol true ``` #### 4. **验证客户端实际使用的协议** 在客户端代码中添加日志: ```python def on_connect(client, userdata, flags, rc, properties): print(f"Connected with protocol: {client._protocol}") # 输出实际协议版本 client.on_connect = on_connect ``` #### 5. **网络抓包分析** 使用 Wireshark 过滤 MQTT 流量: - 检查 CONNECT 报文中的 `Protocol Level` 字段: - `0x04` = MQTT v3.1.1 - `0x05` = MQTT v5.0 - 服务器拒绝时会返回 CONNACK 报文,其中 `Return Code=0x01` 表示协议不支持[^1] #### 6. **升级客户端库** 旧版库可能默认使用不兼容的协议: ```bash # Python 更新 Paho-MQTT pip install --upgrade paho-mqtt ``` > **关键排查点**: > - 服务器是否仅支持 MQTT v5.0,而客户端使用 v3.1? > - 客户端是否未显式指定协议版本(依赖库的默认值可能不兼容)? > - 防火墙/中间件是否修改了协议头? ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值