Avatar of tangteng78
Flag for Malaysia

asked on 

How to do HttpPost request via PostAsync to .NET webservice?

I have the error as attached when I'm trying to do a HttpPost request via PostAsync to a .NET webservice? It is just a simple POST request to update a recordset in the database via a passed parameter [id].

As a note, if I'd changed this request to HttpGet, the update worked as expected. This is to validate that there is no issue on the existing webservice.

The behind code that triggered by click event (Xamarin)
       public async void ClosedPO(object sender, EventArgs e)
            var payload = new Dictionary<string, int>
              {"id", 83832}

            string strPayload = JsonConvert.SerializeObject(payload);
           HttpContent c = new StringContent(strPayload, Encoding.UTF8, "application/json");

            Uri uri = new Uri("https://www.mywebsite.com/webservice1.asmx/PreorderClose");
            var client = new HttpClient();
            var response = await client.PostAsync(uri, c);

            if (response.IsSuccessStatusCode)
                await DisplayAlert("SUCCESS!!", "", "OK");
                await DisplayAlert("FAIL", response.ToString(), "OK");

The code at webservice1.asmx
    Public Function PreorderClose(id As String) As Boolean
        If objProduct.changeProductStatus("preorderclosed", id) Then
            'Successfully updated
            '#Uncomment for production
            Return True
            Return False
        End If

    End Function

Web.config at our server
           <add name="HttpPost"/>
           <add name="HttpGet"/>

Error observed from my actual mobile phone when the button is clicked:

.NET Programming* ASP.NET Core* Xamarin

Avatar of undefined
Last Comment

8/22/2022 - Mon