gpt4 book ai didi

c# - 如何在 xamarin.android 中使用 HttpClient 将数据传递到服务器

转载 作者:行者123 更新时间:2023-11-30 17:32:47 28 4
gpt4 key购买 nike

我正在用 xamarin 开发一个 android 应用程序,现在我想发出 post 请求并将以下数据发送到服务器

Name 
EmailID
Prod_EMAILID
ID

并从服务器获取响应Link of API .我做过这样的事

static async Task CallWebAPIAsync()
{
using (var client = new HttpClient())
{
client.BaseAddress = new Uri("http://localhost:55587/");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

//POST Method
var post = new Post() { Id = 1,NAME="aaaaa222",PROF_EMAILD="aaa@gmail.com",MAILID="asasa@gmail.com" };
HttpResponseMessage responsePost = await client.PostAsJsonAsync("api/Department", post);
if (responsePost.IsSuccessStatusCode)
{
// Get the URI of the created resource.
Uri returnUrl = responsePost.Headers.Location;
Console.WriteLine(returnUrl);
}
}
Console.Read();
}

但它只适用于 GET 方法,现在我想在 Httpclient 中发出 post 请求

最佳答案

这是我在解决方案中使用 HttpClient 的方式

using (var client = new HttpClient())
{
var content = new StringContent(JsonConvert.SerializeObject(myPoco));
content.Headers.ContentType = new MediaTypeHeaderValue("application/json");

var response = await client.PostAsync(new Uri("http://your-url"), content);

/* handle response here*/
};

myPoco 是您的普通 C# 对象

关于c# - 如何在 xamarin.android 中使用 HttpClient 将数据传递到服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45934515/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com