RestSharp 107+版本multipart/form-data文件传输失败问题求助
RestSharp 107+版本multipart/form-data文件上传失败问题排查与解决
我将RestSharp版本从104升级到108后,其他application/json接口运行正常,但使用multipart/form-data传输文件时服务器无法接收到文件。测试发现104.4.0、106.15.0版本可正常工作,而107.3.0、108.0.3版本无法正常传输文件。以下是不同版本的实现代码、请求捕获信息及Nginx错误日志,请问使用107及以上版本传输文件还需进行哪些额外操作?
版本106.15.0 实现代码
var client = new RestClient("https://192.168.0.1/"); client.Timeout = -1; var request = new RestRequest("file", Method.POST); request.AlwaysMultipartFormData = true; request.AddHeader("Authorization", "Auth-Token" + token); request.AddHeader("Content-Type", "multipart/form-data"); request.AddHeader("Accept", "application/json"); request.AddFile("file", path); System.Net.ServicePointManager.ServerCertificateValidationCallback = delegate { return true; }; var response = client.Execute(request); Console.WriteLine(response.Content);
Postman控制台捕获的请求信息
POST /file HTTP/1.1 Authorization: Auth-Token ---- Postman-Token: ecff7025-298c-4b29-8b15-ebd0d679aad8 Host: 192.168.0.1 Content-Type: multipart/form-data; boundary=--------------------------077067924352348455764323 Content-Length: 2409 ----------------------------077067924352348455764323 Content-Disposition: form-data; name="file"; filename="filename.txt" <filename.txt> ----------------------------077067924352348455764323--
版本107.3.0 实现代码
var options = new RestClientOptions("https://192.168.0.1/") { RemoteCertificateValidationCallback = (sender, certificate, chain, sslPolicyErrors) => true, ConfigureMessageHandler = handler => new HttpTracerHandler(handler, new ConsoleLogger(), HttpMessageParts.All), }; var client = new RestClient(options); var request = new RestRequest("file", Method.Post); request.AlwaysMultipartFormData = true; request.AddHeader("Authorization", "Auth-Token" + token); request.AddHeader("Content-Type", "multipart/form-data"); request.AddHeader("Accept", "application/json"); request.AddFile("file", path); var response = client.ExecuteAsync(request).Result; Console.WriteLine(response.Content);
HttpTracer捕获的请求信息
==================== HTTP REQUEST: [POST] ==================== POST https://192.168.0.1/file Authorization: Auth-Token ---- Accept: application/json User-Agent: RestSharp/0.0.0.0 --94000a34-a3fa-4eeb-b803-52eea1c7cbc9 Content-Type: application/octet-stream Content-Disposition: form-data; name="file"; filename="filename.txt" ///////////////////////////////// File contents ... ///////////////////////////////// --94000a34-a3fa-4eeb-b803-52eea1c7cbc9--
Nginx错误日志
upstream timed out (110: Connection timed out) while reading response header from upstream
问题分析与解决步骤
从请求捕获信息对比可发现核心差异:
- 107+版本的请求缺少全局的
Content-Type: multipart/form-data; boundary=xxx请求头,手动添加的该头未生效; - 文件部分被自动添加了
Content-Type: application/octet-stream,可能导致服务器解析异常; - 新版本未设置超时,默认超时时间较短,匹配Nginx的超时日志。
对应的解决操作:
- 移除手动添加的
Content-Type头:RestSharp 107+会自动生成包含boundary的multipart/form-data请求头,手动添加会干扰框架的自动处理逻辑; - 添加超时配置:在
RestClientOptions中设置无超时或足够长的超时时间,对齐旧版本的Timeout = -1:var options = new RestClientOptions("https://192.168.0.1/") { Timeout = TimeSpan.FromMilliseconds(-1), // 无超时 // 其他配置... }; - 指定文件MIME类型:使用
AddFile的重载明确设置文件的Content-Type,避免框架默认的application/octet-stream:request.AddFile("file", path, "text/plain"); // 根据实际文件类型调整,如image/jpeg等 - 避免使用
.Result同步阻塞:推荐用await调用异步方法,防止死锁问题:var response = await client.ExecuteAsync(request);
修改后的示例代码
var options = new RestClientOptions("https://192.168.0.1/") { RemoteCertificateValidationCallback = (sender, certificate, chain, sslPolicyErrors) => true, Timeout = TimeSpan.FromMilliseconds(-1), ConfigureMessageHandler = handler => new HttpTracerHandler(handler, new ConsoleLogger(), HttpMessageParts.All), }; var client = new RestClient(options); var request = new RestRequest("file", Method.Post); request.AlwaysMultipartFormData = true; request.AddHeader("Authorization", "Auth-Token" + token); request.AddHeader("Accept", "application/json"); request.AddFile("file", path, "text/plain"); var response = await client.ExecuteAsync(request); Console.WriteLine(response.Content);
内容的提问来源于stack exchange,提问作者Bap
相关产品推荐
相关产品推荐

