C# 过帐数组值';使用C将s转换为json API#
我正在尝试向一个在线API发送post请求以接收网络数据,它成功地使用了普通的键值,但没有使用数组作为值,并且如果不创建大量额外的类,我无法在internet上找到它 这就是我试图用普通数据发布到url的方式(第三个需要数组而不是单个对象) }C# 过帐数组值';使用C将s转换为json API#,c#,arrays,json,webclient,httpresponse,C#,Arrays,Json,Webclient,Httpresponse,我正在尝试向一个在线API发送post请求以接收网络数据,它成功地使用了普通的键值,但没有使用数组作为值,并且如果不创建大量额外的类,我无法在internet上找到它 这就是我试图用普通数据发布到url的方式(第三个需要数组而不是单个对象) } 有人能在第一个(发送数组值)或第二个(发送原始数据)方面帮助我吗 如果可以,请使用库为您处理序列化。然后您可以这样做(使用Newtonsoft.Json): 与其尝试手动创建字符串,不如使用WebClient。您可能也应该删除WebClient。它被Ht
有人能在第一个(发送数组值)或第二个(发送原始数据)方面帮助我吗 如果可以,请使用库为您处理序列化。然后您可以这样做(使用Newtonsoft.Json):
与其尝试手动创建字符串,不如使用WebClient。您可能也应该删除WebClient。它被HttpClient所取代,顺便说一句,HttpClient还可以将对象序列化为Json
IEnumerable<KeyValuePair<string, string>> queries = new List<KeyValuePair<string, string>>()
{
new KeyValuePair<string, string>("keyA","ValueA"),
new KeyValuePair<string, string>("keyB", ""),
new KeyValuePair<string, string>("KeyC", "ValueCDE"), //array
};
HttpContent q = new FormUrlEncodedContent(queries);
Console.WriteLine(q.ToString());
using (HttpClient client = new HttpClient())
{
using (HttpResponseMessage response = await client.PostAsync(url, q))
{
using (HttpContent content = response.Content)
{
string mycontent = await content.ReadAsStringAsync();
HttpContentHeaders headers = content.Headers;
Console.WriteLine(mycontent);
Console.WriteLine(response);
}
}
}
async static void PostRawRequest(string url)
{
string rawr = @"{
""a"":""a"",
""b"":"""",
""c"": [""C"", ""D"",""F""],
""StickerColor"": ""red""
}";
string result = "";
using (var client = new WebClient())
{
client.Headers[HttpRequestHeader.ContentType] = "application/json";
result = client.UploadString(url, "POST", rawr);
}
Console.WriteLine(result);
using (var client = new HttpClient())
{
var json = JsonConvert.SerializeObject(yourObject);
client.PostAsync(yourUri, new StringContent(json, Encoding.UTF8, "application/json"));
}