C# 如何使用httprequest或httpclient将数据发布到网站api
我正在尝试发布到此api:。并打印api返回的结果 该api的json类型为:C# 如何使用httprequest或httpclient将数据发布到网站api,c#,C#,我正在尝试发布到此api:。并打印api返回的结果 该api的json类型为: {"user":`{"id":37,"username":"lucky","counter_rechecking":0,"user_id":761,"fb_id":"100047460285611"}} 有人能帮我吗。已存在数据和类型数据。谢谢你的帮助。您可以帮助我使用HttpRequest或HttpClient检查此项 private static async Task<HttpResponseMess
{"user":`{"id":37,"username":"lucky","counter_rechecking":0,"user_id":761,"fb_id":"100047460285611"}}
有人能帮我吗。已存在数据和类型数据。谢谢你的帮助。您可以帮助我使用HttpRequest或HttpClient检查此项
private static async Task<HttpResponseMessage> Post(string uri, object input)
{
var content = new StringContent(JsonConvert.SerializeObject(input), Encoding.UTF8, "application/json");
var client = new HttpClient
{
BaseAddress = new Uri(YourBaseApiAddress)
};
var response = await client.PostAsync(uri, content);
if (response.StatusCode == HttpStatusCode.Unauthorized)
{
var newResponse = await Post(uri, input, Authentication);
return newResponse;
}
return response;
}
您的
Json
格式不正确。删除”
在{“user”之后:
您是否尝试过通过postman测试此api?是的,但idk如何在C中发布它?beaucse如果它有user,那么IDi只能发布ID。但不知道如何在USERwhere is RequestJwtToken()中发布ID输入是您要发布的对象。如果您想要使用您拥有的json,您需要删除新的stringContent行,因为这一行将对象转换为json。我删除了您问我的另一行,我从代码中忘记了它。
var response = Post(uri, yourObject).Result;
if (response.IsSuccessStatusCode)
{
var value = await response.Content.ReadAsStringAsync();
}