ASP.NET - way2sms sample code needed via webservice

Asked By mahesh on 01-Oct-14 12:40 PM
I want sample code for way2sms to send sms using webservice.
Mihir Soni replied to mahesh on 01-Oct-14 12:42 PM
way2sms sample code:
string mbno, mseg, ckuser, ckpass;
private HttpWebRequest req;
private CookieContainer cookieCntr;
private string strNewValue;
public static string responseee;
private HttpWebResponse response;
Now write following code on your page load event
protected void Page_Load(object sender, EventArgs e)
   {
   ckuser = "Yourusername";
   ckpass = "YourPassword";
    
   try
   {
     this.req = (HttpWebRequest)WebRequest.Create("http://wwwd.way2sms.com/auth.cl");
     
     this.req.CookieContainer = new CookieContainer();
     this.req.AllowAutoRedirect = false;
     this.req.Method = "POST";
     this.req.ContentType = "application/x-www-form-urlencoded";
     this.strNewValue = "username=" + ckuser + "&password=" + ckpass;
     this.req.ContentLength = this.strNewValue.Length;
     StreamWriter writer = new StreamWriter(this.req.GetRequestStream(), Encoding.ASCII);
     writer.Write(this.strNewValue);
     writer.Close();
     this.response = (HttpWebResponse)this.req.GetResponse();
     this.cookieCntr = this.req.CookieContainer;
     this.response.Close();
     this.req = (HttpWebRequest)WebRequest.Create("http://wwwd.way2sms.com//jsp/InstantSMS.jsp?val=0");
     this.req.CookieContainer = this.cookieCntr;
     this.req.Method = "GET";
     this.response = (HttpWebResponse)this.req.GetResponse();
     responseee = new StreamReader(this.response.GetResponseStream()).ReadToEnd();
     int index = Regex.Match(responseee, "custf").Index;
     responseee = responseee.Substring(index, 0x12);
     responseee = responseee.Replace("\"", "").Replace(">", "").Trim();
     this.response.Close();
    
     pnlsend.Visible = true;
     lblErrormsg.Text = "connected";
   }
   catch (Exception)
   {
     lblErrormsg.Text = "Error connecting to the server...";
   }
   }

Above code will connect your account to way2sms please chnage highlighted code with your username password

Now write following on btnMessageSend_Click event.

protected void btnSend_Click(object sender, EventArgs e)
   {
   mbno = txtTo.Text;//To mobile number
   mseg = txtMessage.Text;//Message
 
   if ((mbno != "") && (mseg != ""))
   {
     try
     {
     this.req = (HttpWebRequest)WebRequest.Create("http://wwwd.way2sms.com//FirstServletsms?custid=");
     this.req.AllowAutoRedirect = false;
     this.req.CookieContainer = this.cookieCntr;
     this.req.Method = "POST";
     this.req.ContentType = "application/x-www-form-urlencoded";
     this.strNewValue = "custid=undefined&HiddenAction=instantsms&Action=" + responseee + "&login=&pass=&MobNo=" + this.mbno + "&textArea=" + this.mseg;
 
     string msg = this.mseg;
     string mbeno = this.mbno;
 
     this.req.ContentLength = this.strNewValue.Length;
     StreamWriter writer = new StreamWriter(this.req.GetRequestStream(), Encoding.ASCII);
     writer.Write(this.strNewValue);
     writer.Close();
     this.response = (HttpWebResponse)this.req.GetResponse();
 
     this.response.Close();
     lblErrormsg.Text = "Message Sent..... " + mbeno + ": " + msg;
     }
     catch (Exception)
     {
     lblErrormsg.Text="Error Sending msg....check your connection...";
     }
   }
   else
   {
     lblErrormsg.Text = "Mob no or msg missing";
   }
   }

I have two textbox and one label on the page.
Anoop S replied to mahesh on 06-Apr-11 01:27 AM
You can use some webservice like
  • For sending SMS to the world: http://www.webservicex.com/sendsmsworld.asmx?WSDL
  • For sending SMS to India: http://www.webservicex.net/SendSMS.asmx?WSDL
for sending SMS with the help of GPRS(General Packet Radio Service ).
refer this for more details and example
http://www.codeproject.com/KB/cpp/SendSmsThroughWS.aspx
Jitendra Faye replied to mahesh on 01-Oct-14 12:43 PM
For sending sms using C# code, first you have to configure your SMS Gateway your usename and password.

please follow below link for more details: http://www.codeproject.com/KB/database/SMS_message_from_SQL.aspx
mahesh replied to Mihir Soni on 01-Oct-14 12:44 PM
On this line  StreamWriter writer = new StreamWriter(this.req.GetRequestStream(), Encoding.ASCII);
i am getting error The name 'Encoding' does not exist in the current context

I have added following headers
using System.Web.Services;
using System.Net;
using System.IO;
Nikhil Mahajan replied to mahesh on 06-Apr-11 02:11 AM
you are getting this error message because you didnt import the namespace

using System.Text;

im port this namespace to use Encoding Class
Nikhil Mahajan replied to mahesh on 01-Oct-14 12:46 PM
An SMS gateway is a way of sending a text message with or without using a mobile (cell) phone. Specifically, it is a device or service offering SMS transit by either transforming messages to mobile network traffic from other media or by allowing transmission or receipt of SMS messages with or without the use of a mobile phone. Typical use of a gateway would be to forward simple email to a mobile phone recipient. It can also be useful in developing web applications that we can interact with via SMS (Short Messaging Service).
dipa ahuja replied to mahesh on 06-Apr-11 07:40 AM
Download the new dll for way2sms :


and this is the code :

CookieContainer cookie = SMSClientLib.Login.Connect("mobileno.""Password"out status);
string[] siteParameters = SMSClientLib.Login.GetSiteParameters(cookie);
string messgeSentResult = SendSMS.Send_Processing("RecMobile""Message!", cookie, siteParameters);
Its Working...!!

Hope this will help you


Karthik K replied to mahesh on 01-Oct-14 12:46 PM
The dll does work with a small change:

string status;

CookieContainer cookie = SMSClientLib.Login.Connect("9841568894", "1234", out status);

string[] siteParameters = SMSClientLib.Login.GetSiteParameters(cookie);

string messgeSentResult = SMSClientLib.SendSMS.Send_Processing("9841568894", "Message!", cookie, siteParameters);
I have highlighted the changes made

Arun replied to mahesh on 03-Aug-11 04:34 AM
Please could you tell how to use the dll in the .net code. It seems it can't be added as a reference in .net code with the error "the dll doenot seems to be a valid .net/com component". Any poinetrs would be appreciated
jismy replied to mahesh on 17-Sep-11 03:07 AM
Hi, I tried Your code Its showing one error that RegEx is not exists ,Can U pls tell me what is mean by REgex in the folloying code
int index = Regex.Match(responseee, "custf").Index;
Dr.Nick S replied to sathish suresh on 01-Feb-12 08:50 PM
  1. Imports System.Net  
  2. Imports System.IO  
  3.   
  4. Partial Class demo  
  5.   Inherits System.Web.UI.Page  
  6.   
  7.      Protected Sub Page_Load(ByVal sender As ObjectByVal e As System.EventArgs) Handles Me.Load  
  8.     SendSMS("18479790553""ToNumber""Do Not Reply: You have an appointment on 1-20-2012 at 3:00pm with Dr. McAndrews at the Carson Office. Please arrive 15 minutes early. ")  
  9.   End Sub  
  10.   
  11.   Sub SendSMS(ByVal FromNumber As StringByVal ToNumber As StringByVal SMS As String)  
  12.     Response.ContentType = "text/xml; charset=utf-8"  
  13.     Dim url As String = "https://www.sendandreceivesms.com/api/"  
  14.     Dim request As HttpWebRequest = DirectCast(WebRequest.Create(url), HttpWebRequest)  
  15.     request.Method = "POST"  
  16.     Dim postData As String = "FromNumber=" & FromNumber  
  17.     postData += "&"  
  18.     postData += "ToNumber=" & ToNumber  
  19.     postData += "&"  
  20.     postData += "SMS=" & SMS  
  21.     Dim byteArray As Byte() = Encoding.UTF8.GetBytes(postData)  
  22.     request.ContentType = "application/x-www-form-urlencoded"  
  23.     request.Headers.Add("APIToken""GoOu9H0h9G")   
  24.     request.ContentLength = byteArray.Length  
  25.     Dim dataStream As Stream = request.GetRequestStream()  
  26.     dataStream.Write(byteArray, 0, byteArray.Length)  
  27.     dataStream.Close()  
  28.     Dim _webresponse As WebResponse = request.GetResponse()  
  29.     dataStream = _webresponse.GetResponseStream()  
  30.     Dim reader As New StreamReader(dataStream)  
  31.     Dim responseFromServer As String = reader.ReadToEnd()  
  32.     Response.Write(responseFromServer)  
  33.     reader.Close()  
  34.     dataStream.Close()  
  35.     'Response.Close()  
  36.   End Sub  
  37. End Class  
Sreelesh Mp replied to jismy on 03-Mar-12 01:25 AM
import the namespace   System.Text.RegularExpressions;
prashant mohite replied to Dr.Nick S on 09-Mar-12 01:44 AM
When i paste this code in DEMO.ASPX then  shows this error.....
I have already add SMSClientLibrary.dll File in BIN folder ( Reference folder )

XML Parsing Error: junk after document element
Location: http://localhost:2610/WebSite7/Demo.aspx
Line Number 3, Column 1:<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
^

Please help me
Rgd.
Prashant
prashant mohite replied to Dr.Nick S on 09-Mar-12 02:07 AM
copy and paste whole the code in Demo.aspx but shows Below error..........

XML Parsing Error: junk after document element
Location: http://localhost:1706/WebSite6/Demo.aspx

Line Number 3, Column 1:<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
^

Any service start for this code or...........

from
Prashant
Dr.Nick S replied to prashant mohite on 09-Mar-12 02:12 AM
That is because your demo.aspx has some html tags that is usually auto generated by visual studio.
The code behind returns and XML response.
The xml response interferes with the HTML tags on the page.

Remove all HTML tags and just leave the page directive in demo.aspx and you will see the XML response.
Which country do you want to primarily use it for?
prashant mohite replied to Dr.Nick S on 11-Mar-12 01:20 AM
is this write code
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Demo.aspx.vb" Inherits="Demo" %>
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
    </div>
    </form>
</body>
-----------------------------------------------------------------

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Demo.aspx.vb" Inherits="Demo" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html>
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    </div>
    </form>
</body>
</html>

I TRIED BOTH THE METHOD MY GIVE ERROR I AM USING THIS SERVICE FROM INDIA.
Dr.Nick S replied to prashant mohite on 11-Mar-12 04:09 AM
Take out this HTML from demo.aspx

<HTML>
<head run at="server">
  <title>Untitled Page</title>
</head>
<body>
  <form id="form1" run at="server">
  <div>
  </div>
  </form>
</body>
</HTML>
Dr.Nick S replied to prashant mohite on 11-Mar-12 04:09 AM
Download the sample app

http://www.sendandreceivesms.com/sendsmsusingvb.net.zip?dl=1
Dr.Nick S replied to prashant mohite on 11-Mar-12 04:12 AM
I am sorry we cannot edit these posts

Delete the following markup from demo.aspx

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<html>
<head runat="server">
  <title>Untitled Page</title>
</head>
<body>
  <form id="form1" runat="server">
  <div>
  </div>
  </form>
</body>
</html>

Dr.Nick S replied to Dr.Nick S on 11-Mar-12 04:12 AM
So your Demo.aspx Should only be

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Demo.aspx.vb" Inherits="Demo" %>
Mahesh Bajjuri replied to Mihir Soni on 01-Oct-14 12:49 PM
when iam trying to use this code , i got an error when connecting to way2sms at this line
----->   this.response = (HttpWebResponse)this.req.GetResponse();
the error is :  The remote server returned an error: (404) Not Found.
 
Deepak Singh replied to Mihir Soni on 18-Aug-17 05:10 AM
showing "The remote server returned an error: (404) Not Found.". Not working. Please tell me about this