【问题标题】:RestSharp AddFile using key and image in C#RestSharp AddFile 在 C# 中使用密钥和图像
【发布时间】:2020-04-20 14:57:30
【问题描述】:

我正在尝试使用接收带有密钥的图像的 Rest 服务。我在 Visual Studio 2017 中使用 RestSharp v.106.10.1。我有以下代码,执行时会返回一条消息,其中包含服务器响应,说明包到达时没有图像。你能帮我看看我做错了什么吗?

这是我的代码

var client = new RestClient("http://localhost:3030/api/upload-image");
    client.Timeout = -1;
    var request = new RestRequest(Method.POST);
    request.AddHeader("Content-Type", "application/x-www-form-urlencoded");
    request.AddFile("images", "/C:/Users/Desktop/AppTestWM/ServidorFTP/450_1000.jpg");
    IRestResponse response = client.Execute(request);
    Console.WriteLine(response.Content);

邮递员的发送方式是这样的。标题 contentType: application/x-www-form-urlencoded 和 form-data 中的正文添加了一个关键的“图像”和图像文件。

这是标题格式

enter image description here

这是正文格式

enter image description here

更多信息

这是邮递员控制台的好答案

发布http://localhost:3000/imagenSoliTag/upload-image

• ▶网络

▶请求标头

用户代理:PostmanRuntime/7.24.1

接受:/

缓存控制:无缓存

邮递员令牌:44761995-f969-417d-9216-c98d2ee38b35

主机:本地主机:3000

接受编码:gzip、deflate、br

连接:保持活动

内容类型:multipart/form-data;边界=------------- -505415277809498326289672

内容长度:20233

• ▶请求正文

▶图片:{…}

_events:{}

_eventsCount:3

▶_可读状态:{…}

自动关闭:真

字节读取:20022

关闭:是的

域:空

fd: 空

标志:“r”

模式:438

路径:“C:\Users\Desktop\AppTestWM\ServidorFTP\450_1000.jpg”

可读:错误

▶响应标头

X-Powered-By: Express

内容类型:应用程序/json;字符集=utf-8

内容长度:71

ETag:W/“47-DTIxeI6y0HsQCMVrZcYol72rDDs”

日期:2020 年 4 月 19 日星期日 23:42:08 GMT

连接:保持活动

• ▶响应正文

响应:“¡Imagen guardada con éxito!”

imagePath: "450_1000.jpg"

在没有图像的情况下进行查询时,这是邮递员控制台的错误答案

发布http://localhost:3000/imagenSoliTag/upload-image

• ▶网络

▶请求标头

用户代理:PostmanRuntime/7.24.1

接受:/

缓存控制:无缓存

邮递员令牌:9903417e-e3f0-467e-a185-046a1043ba45

主机:本地主机:3000

接受编码:gzip、deflate、br

连接:保持活动

内容长度:0

• 请求正文

▶响应标头

X-Powered-By: Express

内容类型:应用程序/json;字符集=utf-8

内容长度:50

ETag:W/“32-Z/sIWz99etyVziu3PXRpXUZOG6c”

日期:2020 年 4 月 19 日星期日 23:44:07 GMT

连接:保持活动

• ▶响应正文

状态码:400

消息:“¡Elija una imagen!”

这是请求中的 Visual Studio 调试

enter image description here

在响应调试中是这样的

enter image description here

【问题讨论】:

    标签: c# post postman restsharp


    【解决方案1】:

    答案就在你的问题中。

    查看邮递员发送的Content-Type:

    这是您使用 RestSharp 发布的请求(使用 Fiddler 4 收集)

    POST /api/upload-image HTTP/1.1
    Accept: application/json, text/json, text/x-json, text/javascript, application/xml, text/xml
    User-Agent: RestSharp/106.0.0.0
    Content-Type: application/x-www-form-urlencoded; boundary=-----------------------------28947758029299
    Host: oguzozgul.com.tr
    Content-Length: 19006
    Accept-Encoding: gzip, deflate
    Connection: Keep-Alive
    

    它实际上是在尝试使用边界发送表单,但是您强制它以 application/x-www-form-urlencoded 发送 Content-Type 标头

    将标头 Content-Type 设置为 multipart/form-data,或者(适用于最新版本),让 RestSharp 自动设置 Content-Type,不强制它使用特定(且不正确)的:(语句设置 Content-Type 标头已删除:)

    var client = new RestClient("http://localhost:3030/api/upload-image");
    client.Timeout = -1;
    var request = new RestRequest(Method.POST);
    request.AddHeader("Content-Type", "multipart/form-data");
    request.AddFile("images", "/C:/Users/Desktop/AppTestWM/ServidorFTP/450_1000.jpg");
    IRestResponse response = client.Execute(request);
    Console.WriteLine(response.Content);
    

    【讨论】:

    • 这并不能解决我的问题。还有其他想法吗?请!!
    • 请提供完整的回复信息,或者debyg信息,无论你有什么信息
    • 它将 Content-Type 发布为 null。你用的是什么版本的restsharp?最终的 NuGet 版本放置了正确的内容类型标头,但您发送的是 null,这就是错误。
    • 我编辑了答案以手动设置 Content-Type 标头。试试这个,如果不行,请升级到最新版本的 RestSharp。
    猜你喜欢
    • 2015-12-28
    • 1970-01-01
    • 2020-11-27
    • 1970-01-01
    • 1970-01-01
    • 2016-05-29
    • 2013-10-15
    • 2019-11-13
    • 1970-01-01
    相关资源
    最近更新 更多