C# RestSharp PUT 方法并发送原始字节 (protobuf)

标签 c# protocol-buffers restsharp

如何使用 HTTP PUT 方法发送字节数组? RestRequest 类的方法 AddFile 发送额外的 header 。 方法 AddParameter 采用 Object 类型。

我是怎么做到的:

byte[] data;
using (var ms = new MemoryStream())
{
    Serializer.Serialize(ms, query);
    data = ms.ToArray();
    ms.Close();
}
var client = new RestClient(ServerPath);
var request = new RestRequest(RequestPath, Method.PUT);
request.AddFile("stream", x => new MemoryStream(data), string.Empty);
client.ExecuteAsync(request, responce => Debug.WriteLine(responce.Content));

但是在服务器端我看到额外的标题

-------------------------------28947758029299 Content-Disposition: form-data; name="stream"; filename="" Content-Type: application/octet-stream [RAW DATA HERE] -------------------------------28947758029299--

额外的标题使查询不可读。我做错了什么?

最佳答案

感谢 Marc Gravell。 解决方案:

        var client = new HttpClient();
        var httpContent = new ByteArrayContent(data);
        client.PutAsync(Path, httpContent);

关于C# RestSharp PUT 方法并发送原始字节 (protobuf),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15067894/

相关文章:

c# - HttpWebResponse.LastModified 的默认值

python - protobuf MessageToJson 删除值为 0 的字段

c# - RestSharp - 数组作为查询字符串参数

c# - RestSharp JsonDeserializer 列表对象

c# - RestSharp 请求中止 - 无法创建 SSL/TLS 安全通道

c# - 如果结果来得太晚,则限制但丢弃结果

c# - 优雅地终止由 QueueUserWorkItem() 分派(dispatch)的线程池任务

c# - 一个控件的多个 UpdateSourceTriggers

java - 在 java 和 scala 中使用 protobufs 的问题

node.js - gRPC Node 服务器: how to load protobuf file from a different Git repo?