Why can't I post from my Xamarin Frorms app to my .net core web api

前端 未结 2 1912
暖寄归人
暖寄归人 2021-01-26 10:40

I have a post method that sends json to my api.

My endpoint works in postman so my problem is in my xamarin post request:

    async void RegisterUser(o         


        
相关标签:
2条回答
  • 2021-01-26 10:55

    So after 2 days of googling i found a solution:

    https://marketplace.visualstudio.com/items?itemName=vs-publisher-1448185.ConveyorbyKeyoti#overview

    now I can post to my api from my xamarin forms project!!!

    thanks for everyone's input

    0 讨论(0)
  • 2021-01-26 11:11

    Please test the Web Api with some REST testing tool like POSTMAN, to verify that it works with your JSON data firstly.

    If your webapi have no problems, please take a look the following code, I can post data to asp.net core webapi successfully, and return value.

     private async void btnpost_Clicked(object sender, EventArgs e)
        {
            ContactModel cm = new ContactModel() {ID=1,Name="cherry",Age=12 };
    
            var httpClient = new HttpClient();
            var content = JsonConvert.SerializeObject(cm);
            HttpContent httpContent = new StringContent(content, Encoding.UTF8);
            httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
            var uri = new Uri("http://xxx.xx.xx.xx:8088/api/Contact");
            HttpResponseMessage response = await httpClient.PostAsync(uri, httpContent);
            if (response.IsSuccessStatusCode)
            {
                var data = await response.Content.ReadAsStringAsync();
    
                string ss = data;
            }
    
        }
    

    Please note: Adding android:usesCleartextTraffic="true" in Mainfeast.xml,because starting with Android 9 (API level 28), cleartext support is disabled by default. if not, you may have Cleartext HTTP traffic not permitted error.

    0 讨论(0)
提交回复
热议问题