如何获取MultipartFormDataContent

时间:2017-01-18 07:03:20

标签: c# system.net

有没有办法获取MultipartFormDataContent的字符串表示以进行调试?

我已尝试var x = multipart.ToString();但它返回了System.Net.Http.MultipartFormDataContent,而不是下面的内容。

-----------------------------9051914041544843365972754266
Content-Disposition: form-data; name="text"

text default
-----------------------------9051914041544843365972754266
Content-Disposition: form-data; name="file1"; filename="a.txt"
Content-Type: text/plain

Content of a.txt.

-----------------------------9051914041544843365972754266
Content-Disposition: form-data; name="file2"; filename="a.html"
Content-Type: text/html

<!DOCTYPE html><title>Content of a.html.</title>

-----------------------------9051914041544843365972754266--

1 个答案:

答案 0 :(得分:0)

你应该尝试创建一个异步函数来测试它,如下所示:

    public static async Task<string> getInput()
    {
         var input = "";

         using (var content = new MultipartFormDataContent("Upload----" + DateTime.Now.ToString()))
         {
             content.Add(new StreamContent(new MemoryStream(Encoding.ASCII.GetBytes("your data"))), 
                                              "file", "upload.xml");

             input = await content.ReadAsStringAsync();

         }

        return input;
    }

    public static void Main(string[] args)
    {
         Console.WriteLine(getInput().Result);
    }