2
I have the following code:
public static string SendSMS(List<string> numbers, string usuario, string message)
{
int count = 0;
string keerpersNumbers = "";
string urlSMS = "https://minhaurl";
string authHeader = "Basic bWFshkjhkj=";
string contentTypeHeader = "application/json";
if (numbers != null && numbers.Count > 0)
{ //separar os numero para o json
foreach (string num in numbers)
{
//keerpersNumbers = num;
if (count > 0)
{
keerpersNumbers = keerpersNumbers + "," + num;
}
else
{
keerpersNumbers = num;
}
count++;
}
if (usuario == null && usuario.Equals(""))
{
usuario = "app";
}
}
var httpWebRequest = (HttpWebRequest)WebRequest.Create(urlSMS);
httpWebRequest.ContentType = contentTypeHeader;
httpWebRequest.Headers.Add("Authorization", authHeader);
httpWebRequest.Method = "POST";
httpWebRequest.Accept = contentTypeHeader;
using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
string json = "{\"from\":\"InfoSMS\", \"to\":\"[5531989872881]\",\"text\":\"Test SMS.\"}";
string contents = JsonConvert.SerializeObject(json);
streamWriter.Write(json);
streamWriter.Flush();
streamWriter.Close();
}
var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
var result = streamReader.ReadToEnd();
}
return httpResponse.StatusCode.ToString();
}
}
Which works, but as you can see, is static. I would like this string json
used the method variables, but, I try to format and always get error 400 from the server.
How to do this? Thank you!
EDIT:
JSON
{
"from":"WineShop",
"to":[
"5531984882881",
"5531984882881"
],
"text":"Wine shop grand opening at Monday 8pm. Don't forget glasses."
}
you are sure that the number array should be an integer represented in a string?
"[5531989872881]\"
– Leandro Angelo
I send a string, as you can see in the message parameters. JSON I will add in the question
– Henrique
So the displayed Json is different from what is stated in the code above the question
– Leandro Angelo
that’s the question, how to format json?
– Henrique