【问题标题】:How to write code to post the file in application/form-data in C# console?如何编写代码以在 C# 控制台的 application/form-data 中发布文件?
【发布时间】:2016-12-15 05:45:42
【问题描述】:

我在 windows 中使用 Post Man 将 application/form-data 中的文件发布到如下网址中。,

  http://{host}:{port}/file

form-data 中的文件是..,

  file "C:/Temp/file.txt"

在 postMan 中是有效的。

但我想编写代码以在 C# 控制台应用程序中执行此操作。

我是新手。所以请任何人提供任何方法来编写代码来处理文件作为 C# 中 Post Method{application/form-data} 中 url 的一部分。 how-to-fill-forms-and-submit-with-webclient-in-c-sharp

我已经检查了附加的链接。

只有代码通过“application/x-www-form-urlencoded”而已。

但我需要 "application/form-data" 的代码。

注意:我已经尝试过该链接中的以下代码,它仅显示 415 Unsupported media Type 错误。

var encoding=new ASCIIEncoding();
var postData="C:/test.csv";
byte[]  data = encoding.GetBytes(postData);

var myRequest =
  (HttpWebRequest)WebRequest.Create("http://localhost/MyIdentity/Default.aspx");
myRequest.Method = "POST";
myRequest.ContentType="application/form-data";
myRequest.ContentLength = data.Length;
var newStream=myRequest.GetRequestStream();
newStream.Write(data,0,data.Length);
newStream.Close();

var response = myRequest.GetResponse();
var responseStream = response.GetResponseStream();
var responseReader = new StreamReader(responseStream);
var result = responseReader.ReadToEnd();

responseReader.Close();
response.Close();

【问题讨论】:

    标签: c# httprequest httpresponse


    【解决方案1】:

    此代码仅适用于“multipart/form-data”

    //Convert each of the three inputs into HttpContent objects
                    byte[] fileBytes = System.IO.File.ReadAllBytes(filePath);
    
                    HttpContent bytesContent = new ByteArrayContent(fileBytes);
    
                    // Submit the form using HttpClient and 
                    // create form data as Multipart (enctype="multipart/form-data")
    
                    using (var client = new System.Net.Http.HttpClient())
                    using (var formData = new MultipartFormDataContent())
                    {
                        // <input type="text" name="filename" />
                        formData.Add(bytesContent, "filename", Path.GetFileName(filePath));
    
                        // Actually invoke the request to the server
    
                        // equivalent to (action="{url}" method="post")
                        var response = client.PostAsync(url, formData).Result;
    
                        // equivalent of pressing the submit button on the form
                        if (!response.IsSuccessStatusCode)
                        {
                            return null;
                        }
                        return response.Content.ReadAsStreamAsync().Result;
                    }
    

    【讨论】:

      【解决方案2】:

      我认为您应该尝试使用 multipart form-data 而不是 application/form-data。 我已成功将 PowerPoint 文件发布到 ASP.NET MVC 控制器,以处理服务器上的文件。 这是link,展示了如何使用多部分表单内容类型上传文件。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-12-16
        • 2015-06-21
        • 1970-01-01
        • 2018-07-10
        • 1970-01-01
        • 2021-12-30
        • 1970-01-01
        相关资源
        最近更新 更多