Click here to Skip to main content
15,747,908 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
aim of my project is send an SMS to mobile phone, when traffic occurs in network. it includes network analyzer. All are perfectly working but SMS will not be sent. It shows A first chance exception of type 'System.Net.WebException' occurred in System.dll error message. how to fix it.?
my coding:
C#
bool verifySettings(RawSocket.PacketArrivedEventArgs packet)
{
   bool verified = false;
   if (Properties.Settings.Default.Protocol.Equals("all"))
      verified = true;
   else
      foreach (String s in Properties.Settings.Default.Protocol.Split(';'))
         if (s.Equals(packet.Protocol))
            verified = true;

   if (!Properties.Settings.Default.MobileNo.Equals("none"))
   {
      if (!(Properties.Settings.Default.DenyFromData.Equals("none") || Properties.Settings.Default.DenyToData.Equals("none")))
      {
         String data = Encoding.ASCII.GetString(packet.ReceiveBuffer);
         if (data.IndexOf(Properties.Settings.Default.DenyFromData) >= -1 || data.IndexOf(Properties.Settings.Default.DenyToData) >= -1)
         {
            sendSMS(Properties.Settings.Default.MobileNo, "Denied Data is travelling from " + packet.DestinationAddress + " to " + packet.OriginationAddress);
         }
      }

      if (!Properties.Settings.Default.DenyFromIP.Equals("none"))
      {
         if (Properties.Settings.Default.DenyFromIP.IndexOf(packet.OriginationAddress) > -1)
            sendSMS(Properties.Settings.Default.MobileNo, "Data from a Denied IP address : " + packet.DestinationAddress);
      }

      if (!Properties.Settings.Default.DenyToIP.Equals("none"))
      {
         if (Properties.Settings.Default.DenyToIP.IndexOf(packet.OriginationAddress) > -1)
            sendSMS(Properties.Settings.Default.MobileNo, "Data to a Denied IP address : "+packet.OriginationAddress);
      }

   }

   return verified;
}
        
bool sendSMS(String MobileNo, String Message)
{
   bool sent = false;
   HttpWebRequest request = default(HttpWebRequest);
   HttpWebResponse response = null;
   string url = null;
   string username = null;
   string password = null;
   string host = null;
   string originator = null;            
            
   try
   {
      host = "http://127.0.0.1:9501";
      originator = "99994905708";    //Sender Mobile Number
      username = "admin";
      password = "abc123";

      url = host + "/api?action=sendmessage&" + "username=" + HttpUtility.UrlEncode(username) + "&password=" + HttpUtility.UrlEncode(password) + "&recipient=" + HttpUtility.UrlEncode(MobileNo) + "&messagetype=SMS:TEXT" + "&messagedata=" + HttpUtility.UrlEncode(Message) + "&originator=" + HttpUtility.UrlEncode(originator) + "&serviceprovider=GSMModem1" + "&responseformat=html";
      request = (HttpWebRequest)WebRequest.Create(url);
      response = (HttpWebResponse)request.GetResponse();
      sent = true;
   }
   catch (Exception)
   {
      sent = false;
   }
   
   return sent;
}
Posted
Updated 23-Dec-13 22:12pm
v3
Comments
Richard MacCutchan 24-Dec-13 4:45am    
Do you have an SMS delivery system running on your PC?
SpiderPravin 24-Dec-13 23:07pm    
No.,what kind of SMS delivery system needed?
Richard MacCutchan 25-Dec-13 4:28am    
You are directing the request to 127.0.0.1, so unless you have an SMS server running on your system that request will be rejected. You need to send it to a system that provides an SMS service.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900