我正在使用API,并且其post方法期望消息值作为数组。我怎么添加它?
using (var wb = new WebClient())
{
var url = "http://apidomain.com/api_post.asp";
var data = new NameValueCollection();
string[] myMessage = new string[] { "my test string" };
data["type"] = "1";
data["u"] = "username";
data["p"] = "password";
data["message"] = myMessage;
data["title"] = "myTitle";
var response = wb.UploadValues(url, "POST", data);
string responseInString = Encoding.UTF8.GetString(response);
Console.WriteLine(responseInString);
Console.ReadLine();
}
我在编译时遇到以下异常
无法将类型string []隐式转换为字符串
答案 0 :(得分:-1)
在您的项目中添加Nuget的Newtonsoft.Json,然后使用following-
data["message"] = JsonConvert.SerializeObject(myMessage);
它将把您的值序列化为字符串,这就是它的样子-
[“我的测试字符串”]