使用 C# 通过 HTTP POST 发送文件
我一直在搜索和阅读相关内容,但找不到任何真正有用的东西。
我正在编写一个小型 C# win 应用程序,允许用户不通过 FTP,而是使用 POST 通过 HTTP 将文件发送到 Web 服务器。 可以将其视为一个 Web 表单,但运行在 Windows 应用程序上。
我使用类似的方法创建了 HttpWebRequest 对象,
HttpWebRequest req = WebRequest.Create(uri) as HttpWebRequest
并设置了 Method
、ContentType
和 ContentLength
属性。 但这就是我能走的最远的地方。
这是我的一段代码:
HttpWebRequest req = WebRequest.Create(uri) as HttpWebRequest;
req.KeepAlive = false;
req.Method = "POST";
req.Credentials = new NetworkCredential(user.UserName, user.UserPassword);
req.PreAuthenticate = true;
req.ContentType = file.ContentType;
req.ContentLength = file.Length;
HttpWebResponse response = null;
try
{
response = req.GetResponse() as HttpWebResponse;
}
catch (Exception e)
{
}
所以我的问题基本上是如何通过 HTTP POST 使用 C# 发送文件(文本文件、图像、音频等)。
谢谢!
I've been searching and reading around to that and couldn't fine anything really useful.
I'm writing an small C# win app that allows user to send files to a web server, not by FTP, but by HTTP using POST. Think of it like a web form but running on a windows application.
I have my HttpWebRequest object created using something like this
HttpWebRequest req = WebRequest.Create(uri) as HttpWebRequest
and also set the Method
, ContentType
and ContentLength
properties. But thats the far I can go.
This is my piece of code:
HttpWebRequest req = WebRequest.Create(uri) as HttpWebRequest;
req.KeepAlive = false;
req.Method = "POST";
req.Credentials = new NetworkCredential(user.UserName, user.UserPassword);
req.PreAuthenticate = true;
req.ContentType = file.ContentType;
req.ContentLength = file.Length;
HttpWebResponse response = null;
try
{
response = req.GetResponse() as HttpWebResponse;
}
catch (Exception e)
{
}
So my question is basically how can I send a fie (text file, image, audio, etc) with C# via HTTP POST.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
使用 .NET 4.5(或 .NET 4.0,通过添加 Microsoft. Net.Http 包(来自 NuGet)有一种更简单的方法来模拟表单请求。 这是一个例子:
Using .NET 4.5 (or .NET 4.0 by adding the Microsoft.Net.Http package from NuGet) there is an easier way to simulate form requests. Here is an example:
仅发送原始文件:
如果您想使用
模拟浏览器表单,那就更难了。 有关 multipart/form-data 答案,请参阅此答案。
To send the raw file only:
If you want to emulate a browser form with an
<input type="file"/>
, then that is harder. See this answer for a multipart/form-data answer.对我来说 client.UploadFile 仍然将内容包装在多部分请求中,所以我必须这样做:
For me
client.UploadFile
still wrapped the content in a multipart request so I had to do it like this:我遇到了同样的问题,下面的代码完美地解决了这个问题:
I had got the same problem and this following code answered perfectly at this problem :
您需要将文件写入请求流:
You need to write your file to the request stream:
要从字节数组发布文件:
To post files as from byte arrays:
您可以像这样直接使用 HttpWebRequest/HttpWebResponse 来完成此操作。
You can do it directly with HttpWebRequest/HttpWebResponse like this.
使用.NET 4.5尝试执行表单POST文件上传。 尝试了上面的大部分方法,但都没有效果。
在这里找到了解决方案
https://www. c-sharpcorner.com/article/upload-any-file-using-http-post-multipart-form-data
但我并不是不热衷,因为我不明白为什么我们仍然需要处理如此低的级别在这些常见用法中进行编程(应该由框架很好地处理)
Using .NET 4.5 trying to perform form POST file upload. Tried most of the methods above but to no avail.
Found the solution here
https://www.c-sharpcorner.com/article/upload-any-file-using-http-post-multipart-form-data
But I am not not keen as I do not understand why we still need to deal with such low level programming in these common usages (should be handled nicely by framework)