POST 数据使用 HttpClient
POST data using HttpClient
我正在尝试 POST 使用 HttpClient 将数据传输到网络,但我无法成功。
这是我的 JSON 网站 api
{
"Categories":[
{
"CategoryID":1,
"Category":"Category 1"
},
{
"CategoryID":2,
"Category":"Category 2"
}
]
}
我正在将类别数据发送到网络,我的网络开发人员在上面发送给我 json 以将数据从 winform 发送到网络
这是我的代码
IEnumerable<KeyValuePair<string, string>> paramt = new List<KeyValuePair<string, string>>()
{
new KeyValuePair<string,string>("CategoryID","1"),
new KeyValuePair<string,string>("Category","Pizza")
};
HttpContent q = new FormUrlEncodedContent(paramt);
using (HttpClient client = new HttpClient())
{
client.DefaultRequestHeaders.Accept.Clear();
client.BaseAddress = new Uri(url);
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", api);
HttpResponseMessage response = client.PostAsync("api/categories", q).Result;
}
抱歉我的英语版主请更新我的问题
谢谢@John 在你的帮助下我做到了
public class CategoryItem
{
public int CategoryID { get; set; }
public string Category { get; set; }
}
public class CategoriesRoot
{
public IList<CategoryItem> Categories { get; set; }
}
var tmp = new CategoriesRoot
{
Categories = new List<CategoryItem> {
new CategoryItem { CategoryID = 1, Category = "Pizza" }
}
};
using (HttpClient client = new HttpClient())
{
client.DefaultRequestHeaders.Accept.Clear();
client.BaseAddress = new Uri(url);
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", api);
HttpResponseMessage response = client.PostAsJsonAsync("api/categories", tmp).Result;
}
我正在尝试 POST 使用 HttpClient 将数据传输到网络,但我无法成功。
这是我的 JSON 网站 api
{
"Categories":[
{
"CategoryID":1,
"Category":"Category 1"
},
{
"CategoryID":2,
"Category":"Category 2"
}
]
}
我正在将类别数据发送到网络,我的网络开发人员在上面发送给我 json 以将数据从 winform 发送到网络
这是我的代码
IEnumerable<KeyValuePair<string, string>> paramt = new List<KeyValuePair<string, string>>()
{
new KeyValuePair<string,string>("CategoryID","1"),
new KeyValuePair<string,string>("Category","Pizza")
};
HttpContent q = new FormUrlEncodedContent(paramt);
using (HttpClient client = new HttpClient())
{
client.DefaultRequestHeaders.Accept.Clear();
client.BaseAddress = new Uri(url);
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", api);
HttpResponseMessage response = client.PostAsync("api/categories", q).Result;
}
抱歉我的英语版主请更新我的问题
谢谢@John 在你的帮助下我做到了
public class CategoryItem
{
public int CategoryID { get; set; }
public string Category { get; set; }
}
public class CategoriesRoot
{
public IList<CategoryItem> Categories { get; set; }
}
var tmp = new CategoriesRoot
{
Categories = new List<CategoryItem> {
new CategoryItem { CategoryID = 1, Category = "Pizza" }
}
};
using (HttpClient client = new HttpClient())
{
client.DefaultRequestHeaders.Accept.Clear();
client.BaseAddress = new Uri(url);
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", api);
HttpResponseMessage response = client.PostAsJsonAsync("api/categories", tmp).Result;
}