Page 1 of 1

SMS integration using c#asp.net

Posted: Thu Apr 13, 2017 12:35 am
by keshs3
Trying to integrate SMS functionality. Sample code below. Both xml and json calls are not working

protected void Page_Load(object sender, EventArgs e)
{
string messagexml = "<sms_message>" +
"<sms_message_id>"+Guid.NewGuid()+"</sms_message_id><to>16xxxxx4277</to><from>6xxxxx7275</from><body>your confirmation number is 12345678765432</body><request_timestamp>"
+ "1491973998</request_timestamp><result_url><result_url><status>queued</status></sms_message>";
string destination = "http://hosting.plumgroup.com/ws/sms/queue.xml";
postXMLData( destination, messagexml);

string JsonData = @"{""object"":{""to"":""16xxxx94277"",""from"":""6xxxxx7275"",""body"":""Testing from VOX Telehealth."",""result_url"":""""}}";

CreateJsonCall(destination, JsonData);
}

public string postXMLData(string destinationUrl, string requestXml)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(destinationUrl);
request.Credentials= new NetworkCredential("uname", "pwd");
byte[] bytes;
bytes = System.Text.Encoding.ASCII.GetBytes(requestXml);
request.ContentType = "text/xml";
request.ContentLength = bytes.Length;
request.Method = "POST";
Stream requestStream = request.GetRequestStream();
requestStream.Write(bytes, 0, bytes.Length);
requestStream.Flush();
requestStream.Close();

//HttpWebResponse response;
//response = (HttpWebResponse)request.GetResponse();
//if (response.StatusCode == HttpStatusCode.OK)
//{
// Stream responseStream = response.GetResponseStream();
// string responseStr = new StreamReader(responseStream).ReadToEnd();
// return responseStr;
//}
return null;
}


public void CreateJsonCall(string URL, string DATA)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URL);
request.Credentials = new NetworkCredential("usename", "pwd");
request.Method = "POST";
request.ContentType = " application/x-www-form-urlencoded";
request.ContentLength = DATA.Length;
StreamWriter requestWriter = new StreamWriter(request.GetRequestStream(), System.Text.Encoding.ASCII);
requestWriter.Write(DATA);
requestWriter.Close();



}

Re: SMS integration using c#asp.net

Posted: Thu Apr 13, 2017 10:06 am
by support
Hi,

The SMS REST API takes in the following POST parameters:

* to
* from
* body
* result_url (optional)

The parameters you are trying to use are the response parameters the API will send back to you after you make the call.

Please refer to the documentation for more details: http://www.plumvoice.com/docs/dev/plumd ... _sms_queue

Regards,
Plum Support

Re: SMS integration using c#asp.net

Posted: Thu Apr 13, 2017 2:33 pm
by keshs3
support wrote: The SMS REST API takes in the following POST parameters:

* to
* from
* body
* result_url (optional)
I am trying to send those 4 params in my json object.

string JsonData = @"{""object"":{""to"":""16xxxx94277"",""from"":""6xxxxx7275"",""body"":""Testing from VOX Telehealth."",""result_url"":""""}}";

Re: SMS integration using c#asp.net

Posted: Thu Apr 13, 2017 2:46 pm
by support
Hi,

The POST request you are trying to make should be in x-www-form-urlencoded format, not in JSON or XML format.

Code: Select all

to=18005555555&from=18005550000&body=thisisatest
Unfortunately, we are not familiar with C#/ASP.NET, but an example in php would be:

Code: Select all

$url = 'http://hosting.plumgroup.com/ws/sms/queue.json';
$fields = array(
	'to' => urlencode("18005555555"),
	'from' => urlencode("18005550000"),
	'body' => urlencode("thisisatest"),
);

//url-ify the data for the POST
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string, '&');

//open connection
$ch = curl_init();

//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERPWD, $username . ":" . $password);
curl_setopt($ch,CURLOPT_POST, count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);

$result = curl_exec($ch);

curl_close($ch);
Regards,
Plum Support