c# - 通过 WebRequest 发送文件

标签 c# http post httpwebrequest system.net.httpwebrequest

我需要使用 C# 发送内容类型为应用程序/八位字节流的文件。

我可以像下面这样创建一个 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);
}

关于c# - 通过 WebRequest 发送文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28244519/

相关文章:

c# - 无法找到该资源。 MVC4

Flutter - 如何在 Dart 中使用 HTTP 发送 POST 请求?

facebook - 如何在图形搜索后获取 Facebook 帖子?

http - Windows 8.1 更新 IIS 8.5 上的公共(public) HTTP 端口转发失败

c++ - 从远程服务器接收数据

python - Django - 一个 View 中相同表单类的多个实例

c# - 允许任何 ASCII 字符的正则表达式模式。但不带空格

c# - EntityType 'posts' 没有定义键。为此 EntityType 定义键

c# - 如何使用亚马逊网络服务获取 "The Offer Listing"?

ruby - HTTP协议(protocol): How does one figure out where a site's homepage is located