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

标签 c# post xamarin.android dotnet-httpclient

我正在用 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/

相关文章:

c# - 修改语法树,然后得到更新的语义模型

php - 如何使用 php 表单文本区域将撇号存储在数据库中?

image-processing - 使用 MVVM Cross 对 Android 中的列表的大位图进行重新采样

c# - 无需在 ViewModel 上使用 Java.Lang.Object 即可自动完成 MVVM 和 Java 转换

c# - 如何将 Windows 窗体元素发送到 wpf 应用程序的后面?

c# - 删除应用程序设置条目

c# - 将 HttpWebResponse 转换为 HttpResponseMessage

html - 使用 HTTP Post 将 secret 数据发送到服务器的安全方法

javascript - React Native - Axios - 尝试上传图片

android - 正确的配置更改处理