带有HttpWebRequest的POST列表

时间:2018-06-22 13:47:49

标签: c# .net httpwebrequest

我正在尝试使用HTTPWebRequest发布C#列表。 我正在使用以下代码块,但获得

  

无法从“ System.Collections.Generic.List”转换为   'char []'

        List<string> mylist = new List<string>();

        HttpWebRequest request = (HttpWebRequest)WebRequest.Create("My_Url");
        request.Method = "POST";
        request.ContentType = "application/json";
        Stream stream = request.GetRequestStream();
        byte[] buffer = Encoding.UTF8.GetBytes(mylist);
        stream.Write(buffer, 0, buffer.Length);
        res = (HttpWebResponse)request.GetResponse();

预先感谢您的帮助

1 个答案:

答案 0 :(得分:1)

根据文档,您应该将stringchar[]char*传递给GetBytes()方法。

您可以使用LINQ

byte[] buffer = mylist.SelectMany(str => Text.Encoding.UTF8.GetBytes(str)).ToArray(); // use needed encoding