How to create an http request in c# Asp.net

Asked

Viewed 321 times

0

I’m wanting to text through this url;

https://www.iagentesms.com.br/webservices/http.php?metodo=envio
&usuario=iagente
&senha=12345
&celular=5199999999
&mensagem=teste+integracao+http

But I don’t know how to make that call. My code:

public static void EnviarSmsOnly(string celular, string mensagem)
    {
        if (celular != null || mensagem != null)
        {
            string endereco = "https://www.iagentesms.com.br/webservices/http.php?metodo=envio";
            string usuario = "&usuario=user";
            string senha = "&senha=senha";
            string celularSoNumero = Util.RemoveNaoNumericos(celular);
            string destinatario = "&celular=" + celularSoNumero;
            string msg = "&mensagem=" + mensagem;
            string url = endereco + usuario + senha + destinatario + msg ;
            //Aqui já não sei fazer.
            WebRequest requisicao = WebRequest.Create(url); ;
            requisicao.ContentType = "application/x-www-form-urlencoded";
            requisicao.Method = "GET";
            requisicao.GetResponseAsync();
        }

    }

API website: https://www.iagente.com.br/api/sms . Grateful from now on.

  • Nothing happens? Is there an error? Is there any tutorial for this request, configuration, website etc...

  • 1

    Nothing happens @Virgilionovic, follow the tutorial: https://www.iagente.com.br/api/sms

  • take a look at this example, I think it will help you. https://docs.microsoft.com/pt-br/dotnet/api/system.net.webrequest?view=netframework-4.8

  • I followed the tutorial but it failed. "Authentication failed because the remote participant closed the transport flow." Line 102: Httpwebresponse Response = (Httpwebresponse)request.Getresponse();

  • You have an error in authentication, ie hard to play local error.

1 answer

1


I did the example below, getting the return from the server. Is returning a credential error message, you probably have to register and use the credentials provided by the service to make a test with real return.

The return of the void for string, as you can see in the code.

public static string EnviarSmsOnly(string celular, string mensagem)
        {
            if (celular != null || mensagem != null)
            {
                string url = $"https://www.iagentesms.com.br/webservices/http.php?metodo=envio&usuario=iagente&senha=12345&celular={celular}&mensagem={mensagem}";

                var requisicao = WebRequest.Create(url);

                // Pega o stream com o contendo retornado pelo servidor.
                var response = (HttpWebResponse)requisicao.GetResponse();
                var dataStream = response.GetResponseStream();

                // Abre o stream usando o StreamReader.
                var reader = new StreamReader(dataStream);

                // Lê o conteudo para retornar o que aconteceu.
                return $"Status: {response.StatusDescription}; Response: {reader.ReadToEnd()}";
            }

            return "Número ou mensagem não informados.";
        }

Below, example of how to call the method:

var response = EnviarSmsOnly("5199999999", "ola mundo");

If you are using an application Console for testing, you can print the result on Prompt using the method Console.Writeline(), example below:

var response = EnviarSmsOnly("5199999999", "ola mundo");
Console.WriteLine(response);

EDIT:

I found this library Iagentesmssharp, I think it might help you do what you want.

  • I haven’t tried it yet, but with the library it’s worth a lot. Thank you.

Browser other questions tagged

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