Network IO error: reqwest::Error { kind: Request, url: "https://www.dmxapi.cn/v1...
错误信息
WebSocket error: Network IO error: reqwest::Error { kind: Request, url: "https://www.dmxapi.cn/v1/chat/completions", source: hyper_util::client::legacy::Error(Connect, Custom { kind: UnexpectedEof, error: "unexpected EOF during handshake" }) } (HTTP Status: 500) (4028)
AI 解决方案
1. 🌐 错误翻译
原文:WebSocket error: Network IO error: reqwest::Error { kind: Request, url: "https://www.dmxapi.cn/v1/chat/completions", source: hyper_util::client::legacy::Error(Connect, Custom { kind: UnexpectedEof, error: "unexpected EOF during handshake" }) } (HTTP Status: 500) (4028)
译文:WebSocket 错误:网络 IO 错误:reqwest::Error { 类型: 请求错误, 请求 URL: "https://www.dmxapi.cn/v1/chat/completions", 来源: hyper_util::client::legacy::Error(连接错误, 自定义错误 { 类型: 意外文件结束, 错误详情: "握手过程中意外收到 EOF" }) }(HTTP 状态码:500)(4028)
2. 🎯 原因分析
https://www.dmxapi.cn/v1/chat/completions 发起连接的过程中,由 reqwest 库和底层的 hyper_util 共同触发,属于客户端与服务端连接建立失败的错误。3. 💡 解决方案
该错误主要由服务端问题导致,客户端侧可做的基础应对措施如下,无法直接通过修改客户端代码强制服务端恢复,以下措施主要用于提升程序稳定性:
// 在发起请求的代码中,添加重试逻辑和超时优化,减少因临时服务端问题导致的中断
use reqwest::{Client, Error as ReqwestError};
use std::time::Duration;
use tokio::time::timeout;
async fn call_api_with_retry() -> Result<(), ReqwestError> {
let client = Client::builder()
.timeout(Duration::from_secs(30)) // 设置连接和请求超时时间
.connect_timeout(Duration::from_secs(10)) // 专门设置连接握手超时
.build()?;
let max_retries = 3;
let mut retry_count = 0;
while retry_count < max_retries {
match timeout(
Duration::from_secs(35), // 总超时控制
client.post("https://www.dmxapi.cn/v1/chat/completions")
.json(&serde_json::json!({/* 你的请求参数 */}))
.send()
).await {
Ok(Ok(response)) => {
if response.status() == 500 {
retry_count += 1;
tokio::time::sleep(Duration::from_secs(2)).await; // 等待后重试
continue;
}
return Ok(());
},
Ok(Err(e)) => return Err(e),
Err(_) => {
retry_count += 1;
tokio::time::sleep(Duration::from_secs(2)).await;
continue;
}
}
}
Err(ReqwestError::new(reqwest::ErrorKind::Request, "请求失败,重试次数耗尽"))
}