Send PHP POST request via Xamarin

Asked

Viewed 513 times

0

I need to submit a request on POST for a php file through this application. This code worked correctly on Windows Forms, however in Xamarin it’s 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.

Mensagem de erro retornada pelo Applicativo

How do I submit a POST request to a php file through Xamarin C#? If anyone can help me with this, I am very grateful!

Note: I have tried it on windows Forms and it worked without any problem, with this same file that is on our server.


I tried to trade for Httpclient, keeps working on Windows Forms, but by Xamarin just doesn’t 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);
            }
        }
  • Hi @Joao, try using Httpclient instead of Webclient..

  • How I would use Httpclient?

  • I tried to change here but again, windows Forms was good, and Xamarin is still going wrong.

  • The method PostAsyncis asynchronous, hence the method void testar() should be async void testar() then call the asynchronous method as follows: await myHttpClient.PostAsync(URL, formContent) @Jonathan

1 answer

0


The method PostAsync 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);
    }
}
  • Thanks for the reply, but now it is returning an error of Nameresolutionfailure apparently in Asyncresult, is there any special way to call an asynchronous method? @rubStackOverflow

  • Reading about the error reported seems to be a difficult problem to treat, test the following 1 verifique se o app tem permissão para usar internet 2 desabilite o wifi e teste pela rede móvel 3 Faça uma segunda tentativa de envio na sequencia com uma pequena pausa de 1 segundo 4 Teste com o IP do servidor http://192.x.x.x/acoes/testphpcsharp.php/ @Jonathan

  • The app is already with internet access, I have both emulator, and mobile (4g and wifi), and is still giving the same problem... http://i.imgur.com/1BJvg5l.png

  • Also tried steps 3 and 4? 3 Faça uma segunda tentativa de envio na sequencia com uma pequena pausa de 1 segundo 4 Teste com o IP do servidor http://192.x.x.x/acoes/testphpcsharp.php/

  • Which version of android is using?

  • 1

    I don’t know why, but I tried this morning and it worked perfectly! Thank you @rubStackOverflow, you helped me a lot!

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.