通过 WebRequest 发送文件

Sending a file via WebRequest

我需要使用 C# 发送内容类型为 application/octet 流的文件。

我可以创建如下所示的 HttpWebRequest:

            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("http://test.com");
            request.Headers.Add("content-type", "application/octet-stream");
            //Add file here?

            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            if (response.StatusCode == HttpStatusCode.Created)
            {
                Console.WriteLine("YAYA");
            }
            else
            {
                Console.WriteLine("OH NO MR BILL!!!!");
            }

如何将文件添加到我的流中?

只需获取请求流,然后从您的源流中复制。

using (var requestStream = request.GetRequestStream())
{
    fileStream.CopyTo(requestStream);
}