Link to home
Start Free TrialLog in
Avatar of tangteng78
tangteng78Flag for Malaysia

asked on

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

Hi,
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");
            }
            else
            {
                await DisplayAlert("FAIL", response.ToString(), "OK");
            }
        }

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

    End Function

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

=======================================================
Error observed from my actual mobile phone when the button is clicked:
=======================================================  
User generated image
ASKER CERTIFIED SOLUTION
Avatar of Ryan Chong
Ryan Chong
Flag of Singapore image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Your web service method returns a boolean value, not a http response status. So when you're checking for http status code, you're expected to check for boolean value instead.

Despite that, the major problem you're doing here is that your web service is a soap-protocol based service which means you're calling it manually via httpclient. You need to reference your service to your project and call it directly as it as it was a local procedure and check for true or false value instead. Your method is not a restful api, it's a web service soap-based api.
Avatar of tangteng78

ASKER

Thanks for the tips. Used the postman.com to slowly debug what's the issue with the POST method.

Apparently that led me to find out that, i need to uncomment out the 1st line from my .asmx file to enable the webservice to be called from a client script

' To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
<System.Web.Script.Services.ScriptService()>      '-> UNCOMMENTED THIS LINE AND IT WORKS
<System.Web.Services.WebService(Namespace:="http://tempuri.org/")> _
<System.Web.Services.WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
<ToolboxItem(False)> _
Public Class WebService1