Send POST request to PHP through Xamarin

0

I need to send a request in POST to a php file through this application. This code worked correctly in Windows Forms , but Xamarin is not working ...

public void testar()
        {
            try
            {
                string nome = txtName.Text;

                string URL = "http://www.italker.com.br/acoes/testphpcsharp.php/";
                WebClient webclient = new WebClient();

                NameValueCollection formData = new NameValueCollection();

                formData["nome"] = nome;

                string responsefromserver = Encoding.UTF8.GetString(webclient.UploadValues(URL, "POST", formData));
                webclient.Dispose();

            }
            catch (Exception ex)
            {
                    alertErrorShow(ex.ToString());
            }
        }

Here is the error message returned.


How do I send a POST request to a php file through Xamarin C #? If anyone can help me in this, I'm very grateful! Home Note: I already tested in windows forms and it worked without any problem, with this same file that is on our server.

I tried switching to HttpClient , continues to work in Windows Forms, but by xamarin just does not work, no error with nothing ... Did I do something wrong?

 public void testar()
        {
            try
            {
                string nome = txtName.Text;

                string URL = "http://www.italker.com.br/acoes/testphpcsharp.php/";
                var formContent = new FormUrlEncodedContent(new[]
                {
                    new KeyValuePair<string, string>("nome", nome)
                });

                var myHttpClient = new HttpClient();
                var response = myHttpClient.PostAsync(URL, formContent);
                alertShow(response.ToString(), true);
            }
            catch (Exception ex)
            {
                alertShow(ex.ToString(), false);
            }
        }
    
asked by anonymous 03.09.2016 / 15:42

1 answer

0

The PostAsync method is asynchronous, so the void test () method should be async void testar() then call the asynchronous method as follows: await myHttpClient.PostAsync(URL, formContent)

  

See how it would look in the code example below:

public async void testar()
{
    try
    {
        string nome = txtName.Text;

        string URL = "http://www.italker.com.br/acoes/testphpcsharp.php/";
        var formContent = new FormUrlEncodedContent(new[]
        {
            new KeyValuePair<string, string>("nome", nome)
        });

        var myHttpClient = new HttpClient();
        var response = await myHttpClient.PostAsync(URL, formContent);
        alertShow(response.ToString(), true);
    }
    catch (Exception ex)
    {
        alertShow(ex.ToString(), false);
    }
}
    
04.09.2016 / 00:09