Hi,
I’m trying to send a text message (SMS) with a CANxtra. I use the sms gateway provided by my telephone company: [url]https://www.bibob.dk/SmsSender.asmx[/url].
I am trying to modify this Powershell script: [url]http://www.xipher.dk/WordPress/?p=616[/url].
The only thing I have changed is that I have added the “deliveryReport”, as I thought it might be needed. See this link:
[url]https://www.bibob.dk/SmsSender.asmx?op=SendMessage[/url], I have also changed the server to https://www.bibob.dk/ instead of https://www.bibob.dk/SmsSender.asmx, as the host header is read only.
Here is my code (I have replaced my username and password with x’s):
using System;
using System.IO;
using System.Net;
using System.Text;
using Microsoft.SPOT;
namespace SMSSender
{
class SMS
{
public void SendSMS(int number, string message)
{
if (number.ToString().Length != 8)
{
Debug.Print("Number has to be 8 digits");
return;
}
string username = "xxxxxxxx";//My phonenumber
string password = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";//Converted to MD5 Hash
string server = "https://www.bibob.dk/";
string XMLString = "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
XMLString += "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"";
XMLString += "xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"";
XMLString += "xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">";
XMLString += "<soap:Body><SendMessage xmlns=\"http://www.bibob.dk/\">";
XMLString += "<cellphone>"+ username + "</cellphone>";
XMLString += "<password>" + password + "</password>";
XMLString += "<smsTo><string>" + number.ToString() +"</string></smsTo>";
XMLString += "<smscontents>" + message + "</smscontents>";
XMLString += "<fromNumber>" + username + "</fromNumber>";
XMLString += "<deliveryReport>" + false + "</deliveryReport>";
XMLString += "</SendMessage></soap:Body></soap:Envelope>";
Byte[] bytesToSend = Encoding.UTF8.GetBytes(XMLString);
HttpWebRequest myWebRequest = (HttpWebRequest)WebRequest.Create(server) as HttpWebRequest;
myWebRequest.Method = "POST";
myWebRequest.Headers.Add("Man", "POST /SmsSender.asmx HTTP/1.1");
myWebRequest.ContentType = "text/xml; charset=utf-8";
myWebRequest.ContentLength = bytesToSend.Length;
myWebRequest.Headers.Add("SOAPAction", "http://www.bibob.dk/SendMessage");
using(Stream outputStream = myWebRequest.GetRequestStream())
outputStream.Write(bytesToSend, 0, bytesToSend.Length);
}
}
}
Bassicly I got two questions:
At this link: [url]https://www.bibob.dk/SmsSender.asmx?op=SendMessage[/url] under SOAP 1.1 it says that the first header must be:
POST /SmsSender.asmx HTTP/1.1
He does it this way in the Powershell script:
$objHTTP.setRequestHeader(‘Man’, ‘POST’ + ’ ’ + $SoapServer + ’ HTTP/1.1’)
Is it correct just to do in C#?
myWebRequest.Headers.Add(“Man”, “POST /SmsSender.asmx HTTP/1.1”);
Is it correct to use a HttpWebRequest? I though about using sockets, and then just send the data, but then I can not set the headers, needed for it to work.
Btw if I run the code, if throws an exception when trying to get the request stream:
using(Stream outputStream = myWebRequest.GetRequestStream())
.
This is the debug output:
#### Exception System.Net.Sockets.SocketException - CLR_E_FAIL (1) ####
#### Message:
#### Microsoft.SPOT.Net.SocketNative::getaddrinfo [IP: 0000] ####
#### System.Net.Dns::GetHostEntry [IP: 0008] ####
#### System.Net.HttpWebRequest::EstablishConnection [IP: 00e1] ####
#### System.Net.HttpWebRequest::SubmitRequest [IP: 0013] ####
#### System.Net.HttpWebRequest::GetRequestStream [IP: 0008] ####
#### SMSSender.SMS::SendSMS [IP: 0112] ####
#### CANxtra_Webserver_LCD.Program::Main [IP: 006a] ####
#### SocketException ErrorCode = -1
#### SocketException ErrorCode = -1
A first chance exception of type 'System.Net.Sockets.SocketException' occurred in Microsoft.SPOT.Net.dll
#### SocketException ErrorCode = -1
#### SocketException ErrorCode = -1
#### Exception System.Net.WebException - 0x00000000 (1) ####
#### Message: host not available
#### System.Net.HttpWebRequest::EstablishConnection [IP: 00f1] ####
#### System.Net.HttpWebRequest::SubmitRequest [IP: 0013] ####
#### System.Net.HttpWebRequest::GetRequestStream [IP: 0008] ####
#### SMSSender.SMS::SendSMS [IP: 0112] ####
#### CANxtra_Webserver_LCD.Program::Main [IP: 006a] ####
A first chance exception of type 'System.Net.WebException' occurred in System.Http.dll
A first chance exception of type 'System.Net.WebException' occurred in System.Http.dll
An unhandled exception of type 'System.Net.WebException' occurred in System.Http.dll
Additional information: host not available
I do not have much experience with XML, so any help would be much appreciated