Doubt about HTTP POST

Asked

Viewed 399 times

3

I’m trying to text through a Webservice.

To send a single SMS is simple. Just use the URL in this format:

https://xxxxx.com/sendMessage?messageText=mensagemdeteste&destination=552199998888&key=XXXX

But according to the Webservice documentation I can send several at the same time with this URL format:

https://xxxxx.com/sendMessageBatch?messageText=mensagemdeteste&key=XXXX

It says I should add the numbers to the HTTP POST BODY:

This option Allows one message to be sent to more than one phone number. Destination Numbers need to be in the body of the HTTP POST.

The body of the POST should be composed of Lines containing the Destination phone Numbers (line breaks only as separators):

destination1\\n<destination2>\\n<destination3>\\n<destinationN>

But how does it work?? I can only work on this body via server, via C# for example??

2 answers

7


Yes, it is possible in C#. Your code would look like the following sample:

var destinos = "5555-5555\n5555-5556\n5555-9999"; //<-Números que receberão a msg

byte[] buffer= Encoding.ASCII.GetBytes(destinos);

var webReq = (HttpWebRequest) WebRequest.Create("https://xxxxx.com/sendMessageBatch?messageText=mensagemdeteste&key=XXXX");

webReq.Method = "POST";
// webReq.ContentType = "content-type"; <- caso necessário
webReq.ContentLength = buffer.Length;

var reqStream = webReq.GetRequestStream();
reqStream.Write(buffer, 0, buffer.Length);
reqStream.Close();

var webResp = (HttpWebResponse) webReq.GetResponse();

A similar code in jQuery:

$.ajax({
        url: 'https://xxxxx.com/sendMessageBatch?messageText=mensagemdeteste&key=XXXX',
        type: 'POST',
        data: '5555-5555\n5555-5556\n5555-9999',
        success: function(data, textStatus, xhr) {
                //Caso sua operação tenha sido bem-sucedida
            }
        }
  • Cool, so just to complement... The url can send parameters, that’s one thing, and the http post body is something else and can be modified through a server language. That’s more or less it?

  • But the body, can I only modify via server? And based on my example Content Type would be necessary?

  • Thank you very much! I find it interesting to complement the answer with these remarks, but feel free!

  • if I want to put the url parameters inside the "date:" together with the numbers is possible?

  • @Joaopaulo Technically it is possible. Whether it will work or not depends on the implementation on the side of the provider of this service.

  • Onosendai, webReq.Getrequeststream() brings me a json object. But I saw in the documentation that if I want I can return in xml through "application/xml". You know how I can force it to bring me in xml?

  • 1

    Was that right!!

  • 1

    @Joaopaulo Good, I’m glad it worked - and I appreciate the generosity. =)

Show 3 more comments

2

Since the launch of C# 4.0, it is standard to use HttpClient (part of the nuget Microsoft.Net.Http) for web requests. This class creates a unified API for sending/receiving HttpRequestMessages and HttpResponseMessages.

string[] destinos = {"destino1", "destino2", "destino3"};
var destinosStr = string.Join(Environment.NewLine, destinos);

var content = new StringContent(destinosStr);
var client = new HttpClient();

var response = await client.PostAsync("https://xxxxx.com/sendMessageBatch?messageText=mensagemdeteste&key=XXXX", content);

The API is much more clean and easy to use than the WebRequest. No need to work with buffers and byte arrays, and supports asynchronous tasks .

Browser other questions tagged

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