使用 C# 通过 HTTP POST 发送文件

Posted

技术标签:

【中文标题】使用 C# 通过 HTTP POST 发送文件【英文标题】:Send a file via HTTP POST with C# 【发布时间】:2010-11-11 00:17:55 【问题描述】:

我一直在搜索和阅读这方面的内容,但没有找到任何真正有用的东西。

我正在编写一个小型 C# win 应用程序,它允许用户将文件发送到 Web 服务器,而不是通过 FTP,而是通过 HTTP 使用 POST。可以把它想象成一个 Web 表单,但运行在 Windows 应用程序上。

我的 HttpWebRequest 对象是使用类似这样的东西创建的

HttpWebRequest req = WebRequest.Create(uri) as HttpWebRequest 

并设置MethodContentTypeContentLength 属性。但这就是我能做到的。

这是我的一段代码:

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# 发送文件(文本文件、图像、音频等)。

谢谢!

【问题讨论】:

请查看***.com/questions/15738847/… 8 年后我也有同样的需求:我有一个网站接受文件上传,显示一些关于它的内容,并允许用户下载报告,如果他们愿意的话,但现在他们想要一个 API,所以这种方法似乎是对客户端实现进行白痴验证的最简单方法:他们只需向我发送一个字节数组,然后我在 API 调用中处理服务器上所有隐含的用户操作,并返回报告他们最终想要取回的文件而不是网站体验。无需完全重构网站及其报告生成过程来处理此问题。 【参考方案1】:

你可以像这样直接用HttpWebRequest/HttpWebResponse来做。

        string serviceUrl = string.Format("0/upload?param=1", "http://127.0.0.1:8080", HttpUtility.UrlEncode(parameter));
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(serviceUrl);
        request.Method = "POST";
        request.KeepAlive = true;
        
        FileStream file = File.OpenRead(pathToFile);
        request.ContentLength = file.Length;

        file.Seek(0, SeekOrigin.Begin);
        file.CopyTo(request.GetRequestStream());

        HttpWebResponse response = (request.GetResponse() as HttpWebResponse);
        StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
        string responseText = reader.ReadToEnd();

【讨论】:

【参考方案2】:

使用 .NET 4.5 尝试执行表单 POST 文件上传。尝试了上面的大部分方法,但没有成功。 在这里找到了解决方案 https://www.c-sharpcorner.com/article/upload-any-file-using-http-post-multipart-form-data

但我并不热衷,因为我不明白为什么我们仍然需要在这些常见用法中处理如此低级的编程(应该由框架很好地处理)

【讨论】:

其实地址已经改了。新地址是docs.microsoft.com/en-us/dotnet/api/…【参考方案3】:

使用 .NET 4.5(或 .NET 4.0,通过从 NuGet 添加 Microsoft.Net.Http 包)有一种更简单的方法来模拟表单请求。这是一个例子:

private async Task<System.IO.Stream> Upload(string actionUrl, string paramString, Stream paramFileStream, byte [] paramFileBytes)

    HttpContent stringContent = new StringContent(paramString);
    HttpContent fileStreamContent = new StreamContent(paramFileStream);
    HttpContent bytesContent = new ByteArrayContent(paramFileBytes);
    using (var client = new HttpClient())
    using (var formData = new MultipartFormDataContent())
    
        formData.Add(stringContent, "param1", "param1");
        formData.Add(fileStreamContent, "file1", "file1");
        formData.Add(bytesContent, "file2", "file2");
        var response = await client.PostAsync(actionUrl, formData);
        if (!response.IsSuccessStatusCode)
        
            return null;
        
        return await response.Content.ReadAsStreamAsync();
    

【讨论】:

如果可能的话可以显示一个调用这个方法的简单例子吗? paramString 参数是什么? 谢谢,非常全面的例子! @eranotzap paramString 是要发送的参数的实际值form.Add 的第三个参数是可选的,只对文件有用。 @Liam,我完全同意。异步代码已从我 2013 年的答案中删除,以保持简单。将其改回异步方法已在我的待办事项列表中,因为此时大多数 C# 开发人员应该对它感到满意。 @Ammar,我不知道,我认为您必须将文件读入流或字节 [] 并分别使用 StreamContent 或 ByteArrayContent。【参考方案4】:
     public string SendFile(string filePath)
            
                WebResponse response = null;
                try
                
                    string sWebAddress = "Https://www.address.com";

                    string boundary = "---------------------------" + DateTime.Now.Ticks.ToString("x");
                    byte[] boundarybytes = System.Text.Encoding.ASCII.GetBytes("\r\n--" + boundary + "\r\n");
                    HttpWebRequest wr = (HttpWebRequest)WebRequest.Create(sWebAddress);
                    wr.ContentType = "multipart/form-data; boundary=" + boundary;
                    wr.Method = "POST";
                    wr.KeepAlive = true;
                    wr.Credentials = System.Net.CredentialCache.DefaultCredentials;
                    Stream stream = wr.GetRequestStream();
                    string formdataTemplate = "Content-Disposition: form-data; name=\"0\"\r\n\r\n1";

                    stream.Write(boundarybytes, 0, boundarybytes.Length);
                    byte[] formitembytes = System.Text.Encoding.UTF8.GetBytes(filePath);
                    stream.Write(formitembytes, 0, formitembytes.Length);
                    stream.Write(boundarybytes, 0, boundarybytes.Length);
                    string headerTemplate = "Content-Disposition: form-data; name=\"0\"; filename=\"1\"\r\nContent-Type: 2\r\n\r\n";
                    string header = string.Format(headerTemplate, "file", Path.GetFileName(filePath), Path.GetExtension(filePath));
                    byte[] headerbytes = System.Text.Encoding.UTF8.GetBytes(header);
                    stream.Write(headerbytes, 0, headerbytes.Length);

                    FileStream fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read);
                    byte[] buffer = new byte[4096];
                    int bytesRead = 0;
                    while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)
                        stream.Write(buffer, 0, bytesRead);
                    fileStream.Close();

                    byte[] trailer = System.Text.Encoding.ASCII.GetBytes("\r\n--" + boundary + "--\r\n");
                    stream.Write(trailer, 0, trailer.Length);
                    stream.Close();

                    response = wr.GetResponse();
                    Stream responseStream = response.GetResponseStream();
                    StreamReader streamReader = new StreamReader(responseStream);
                    string responseData = streamReader.ReadToEnd();
                    return responseData;
                
                catch (Exception ex)
                
                    return ex.Message;
                
                finally
                
                    if (response != null)
                        response.Close();
                
            

【讨论】:

【参考方案5】:

对我来说 client.UploadFile 仍然将内容包装在一个多部分请求中,所以我必须这样做:

using (WebClient client = new WebClient())

    client.Headers.Add("Content-Type", "application/octet-stream");
    using (Stream fileStream = File.OpenRead(filePath))
    using (Stream requestStream = client.OpenWrite(new Uri(fileUploadUrl), "POST"))
    
        fileStream.CopyTo(requestStream);
    

【讨论】:

【参考方案6】:

从字节数组发布文件:

private static string UploadFilesToRemoteUrl(string url, IList<byte[]> files, NameValueCollection nvc) 

        string boundary = "----------------------------" + DateTime.Now.Ticks.ToString("x");

        var request = (HttpWebRequest) WebRequest.Create(url);
        request.ContentType = "multipart/form-data; boundary=" + boundary;
        request.Method = "POST";
        request.KeepAlive = true;
        var postQueue = new ByteArrayCustomQueue();

        var formdataTemplate = "\r\n--" + boundary + "\r\nContent-Disposition: form-data; name=\"0\";\r\n\r\n1";

        foreach (string key in nvc.Keys) 
            var formitem = string.Format(formdataTemplate, key, nvc[key]);
            var formitembytes = Encoding.UTF8.GetBytes(formitem);
            postQueue.Write(formitembytes);
        

        var headerTemplate = "\r\n--" + boundary + "\r\n" +
            "Content-Disposition: form-data; name=\"0\"; filename=\"1\"\r\n" + 
            "Content-Type: application/zip\r\n\r\n";

        var i = 0;
        foreach (var file in files) 
            var header = string.Format(headerTemplate, "file" + i, "file" + i + ".zip");
            var headerbytes = Encoding.UTF8.GetBytes(header);
            postQueue.Write(headerbytes);
            postQueue.Write(file);
            i++;
        

        postQueue.Write(Encoding.UTF8.GetBytes("\r\n--" + boundary + "--"));

        request.ContentLength = postQueue.Length;

        using (var requestStream = request.GetRequestStream()) 
            postQueue.CopyToStream(requestStream);
            requestStream.Close();
        

        var webResponse2 = request.GetResponse();

        using (var stream2 = webResponse2.GetResponseStream())
        using (var reader2 = new StreamReader(stream2)) 

            var res =  reader2.ReadToEnd();
            webResponse2.Close();
            return res;
        
    

public class ByteArrayCustomQueue 

    private LinkedList<byte[]> arrays = new LinkedList<byte[]>();

    /// <summary>
    /// Writes the specified data.
    /// </summary>
    /// <param name="data">The data.</param>
    public void Write(byte[] data) 
        arrays.AddLast(data);
    

    /// <summary>
    /// Gets the length.
    /// </summary>
    /// <value>
    /// The length.
    /// </value>
    public int Length  get  return arrays.Sum(x => x.Length);  

    /// <summary>
    /// Copies to stream.
    /// </summary>
    /// <param name="requestStream">The request stream.</param>
    /// <exception cref="System.NotImplementedException"></exception>
    public void CopyToStream(Stream requestStream) 
        foreach (var array in arrays) 
            requestStream.Write(array, 0, array.Length);
        
    

【讨论】:

【参考方案7】:

我遇到了同样的问题,下面的代码完美地解决了这个问题:

//Identificate separator
string boundary = "---------------------------" + DateTime.Now.Ticks.ToString("x");
//Encoding
byte[] boundarybytes = System.Text.Encoding.ASCII.GetBytes("\r\n--" + boundary + "\r\n");

//Creation and specification of the request
HttpWebRequest wr = (HttpWebRequest)WebRequest.Create(url); //sVal is id for the webService
wr.ContentType = "multipart/form-data; boundary=" + boundary;
wr.Method = "POST";
wr.KeepAlive = true;
wr.Credentials = System.Net.CredentialCache.DefaultCredentials;

string sAuthorization = "login:password";//AUTHENTIFICATION BEGIN
byte[] toEncodeAsBytes = System.Text.ASCIIEncoding.ASCII.GetBytes(sAuthorization);
string returnValue = System.Convert.ToBase64String(toEncodeAsBytes);
wr.Headers.Add("Authorization: Basic " + returnValue); //AUTHENTIFICATION END
Stream rs = wr.GetRequestStream();


string formdataTemplate = "Content-Disposition: form-data; name=\"0\"\r\n\r\n1"; //For the POST's format

//Writting of the file
rs.Write(boundarybytes, 0, boundarybytes.Length);
byte[] formitembytes = System.Text.Encoding.UTF8.GetBytes(Server.MapPath("questions.pdf"));
rs.Write(formitembytes, 0, formitembytes.Length);

rs.Write(boundarybytes, 0, boundarybytes.Length);

string headerTemplate = "Content-Disposition: form-data; name=\"0\"; filename=\"1\"\r\nContent-Type: 2\r\n\r\n";
string header = string.Format(headerTemplate, "file", "questions.pdf", contentType);
byte[] headerbytes = System.Text.Encoding.UTF8.GetBytes(header);
rs.Write(headerbytes, 0, headerbytes.Length);

FileStream fileStream = new FileStream(Server.MapPath("questions.pdf"), FileMode.Open, FileAccess.Read);
byte[] buffer = new byte[4096];
int bytesRead = 0;
while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)

    rs.Write(buffer, 0, bytesRead);

fileStream.Close();

byte[] trailer = System.Text.Encoding.ASCII.GetBytes("\r\n--" + boundary + "--\r\n");
rs.Write(trailer, 0, trailer.Length);
rs.Close();
rs = null;

WebResponse wresp = null;
try

    //Get the response
    wresp = wr.GetResponse();
    Stream stream2 = wresp.GetResponseStream();
    StreamReader reader2 = new StreamReader(stream2);
    string responseData = reader2.ReadToEnd();

catch (Exception ex)

    string s = ex.Message;

finally

    if (wresp != null)
    
        wresp.Close();
        wresp = null;
    
    wr = null;

【讨论】:

如何接收数据并将文件保存到另一端的磁盘?【参考方案8】:

您需要将文件写入请求流:

using (var reqStream = req.GetRequestStream()) 
    
    reqStream.Write( ... ) // write the bytes of the file

【讨论】:

【参考方案9】:

只发送原始文件

using(WebClient client = new WebClient()) 
    client.UploadFile(address, filePath);

如果您想用&lt;input type="file"/&gt; 模拟浏览器表单,那就更难了。有关多部分/表单数据的答案,请参阅 this answer。

【讨论】:

(你当然可以像往常一样添加标题/凭据/等) 谢谢,我用过一些简单的东西,但没有用。现在,正如你所说,我确实需要模拟一个浏览器输入文件,类似于 . 我使用了上面的代码并得到了类似的错误:用户代码未处理参数异常:“不支持 URI 格式。”。我怎样才能做到这一点? protected void Page_Load(object sender, EventArgs e) string address="http:www.testproject.com/SavedFiles";字符串文件路径=@"D:\test\FileOperations\testfile.txt";使用 (WebClient client = new WebClient()) client.UploadFile(address, filepath); @Sudha 你试过使用真实的网址吗? http://www.testproject.com/SavedFiles - 注意//

以上是关于使用 C# 通过 HTTP POST 发送文件的主要内容,如果未能解决你的问题,请参考以下文章

.Net(C#)后台发送Get和Post请求的几种方法总结

使用 HttpClient 和 C# 在 post 请求中发送 json

c#如何post文件流

C# 如何通过 TCP 为 HTTP 发送文件?

通过 http 异步发送数据

从 C# 向 URL 发送 POST 请求 [重复]