PageRenderTime 55ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

/Way2Sms/Way2Sms.cs

#
C# | 288 lines | 159 code | 69 blank | 60 comment | 11 complexity | 2ed7a64f4a8764182724182f5886020b MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.IO;
  5. using System.Net;
  6. using System.Runtime.Serialization.Formatters.Binary;
  7. using System.Text;
  8. using Way2SmsApp.Utils;
  9. namespace Way2SmsApp.Way2Sms
  10. {
  11. [Serializable()]
  12. public class W2SContact
  13. {
  14. public string Name;
  15. public string MobileNumber;
  16. public string Group;
  17. public string ToString()
  18. {
  19. return Name + " " + MobileNumber;
  20. }
  21. }
  22. [Serializable()]
  23. public class W2SAddressBook
  24. {
  25. public List<W2SContact> Contacts = new List<W2SContact>();
  26. public Dictionary<string, List<W2SContact>> ContactGroups = new Dictionary<string, List<W2SContact>>();
  27. }
  28. public class Way2SmsAPI
  29. {
  30. //string URL_W2S_LOGIN = "http://site6.way2sms.com/auth.cl";
  31. string URL_W2S_LOGIN = "http://site4.way2sms.com/Login1.action";
  32. //string URL_W2S_LOGIN_REFERER = "http://site6.way2sms.com/content/index.html";
  33. string URL_W2S_LOGIN_REFERER = "http://site4.way2sms.com/content/index.html";
  34. //username 99999999999
  35. //password 0000
  36. public W2SAddressBook AddressBook = new W2SAddressBook();
  37. //http://site4.way2sms.com/Main.action?id=
  38. //http://site4.way2sms.com/jsp/DashBoard.jsp
  39. //http://site4.way2sms.com/QuickContacts POST folder DashBoard
  40. // After login, resulting url is of the form as below
  41. //http://site6.way2sms.com/./jsp/Main.jsp?id=CFB24FF4EF2CCCF70AA8F661AF396EDF.w810
  42. //http://site6.way2sms.com/jsp/Main.jsp?id=10256EDCF0631F9C0FAA5A21EEB3FC4A.w803
  43. // Get Action Value Url
  44. string URL_W2S_GETACTION = "http://site4.way2sms.com/jsp/InstantSMS.jsp";
  45. // Action parse string
  46. // <input type="hidden" name="Action" id="Action" value="sdf5445fdg" />
  47. // Now to send sms
  48. //string URL_W2S_QUICKSMS = "http://site6.way2sms.com/FirstServletsms";
  49. string URL_W2S_QUICKSMS = "http://site4.way2sms.com/quicksms.action";
  50. //string URL_W2S_QUICKSMS_REFERER = "http://site6.way2sms.com/jsp/InstantSMS.jsp";
  51. string URL_W2S_QUICKSMS_REFERER = "http://site4.way2sms.com/jsp/InstantSMS.jsp";
  52. //HiddenAction instantsms
  53. //catnamedis Birthday
  54. //Action sdf5445fdg
  55. //chkall on
  56. //MobNo 9199999999999
  57. //textArea helllllooo fejkeww,f fwf,we fw,g ,ggf w,f,.f'';;'er;q'q;w
  58. //guid username
  59. //gpwd *******
  60. //yuid username
  61. //ypwd *******
  62. // Future sms
  63. //http://site6.way2sms.com/ScheduleSMS
  64. //schnAction schinsert
  65. //mobno 919999999999
  66. //smsg its future sms...
  67. //sdate 2011/06/29
  68. //stime 11:45~PM
  69. //http://site6.way2sms.com/jsp/sheduleconfirm.jsp
  70. //schmobile 99999999999
  71. //schtext its future sms...
  72. //schhour 11
  73. //schminute 45
  74. //schsession PM
  75. private string URL_W2S_USERCONTACTS_GET = "http://site2.way2sms.com/jsp/UserContacts.jsp";
  76. private string URL_W2S_USERCONTACTS_POST = "http://site2.way2sms.com/FBMain";
  77. // faction checkFB
  78. // wfb main
  79. private string URL_W2S_DASHBOARD = "http://site4.way2sms.com/jsp/DashBoard.jsp";
  80. private string URL_W2S_QUICKCONTACTS = "http://site4.way2sms.com/QuickContacts";
  81. // folder DashBoard
  82. private string URL_W2S_USERCONTACTS_REFERRER = "http://site2.way2sms.com/Main.action?id=";//"DC523D85FCF9F8096BF835DE581FC23C.w809";
  83. private string URL_W2S_ID = "";
  84. string _userName;
  85. string _password;
  86. string _action;
  87. bool IsLoggedIn;
  88. CookieContainer cookies = new CookieContainer();
  89. private string _addrbookFile;
  90. public bool GetAddressBook()
  91. {
  92. int retryCnt = 0;
  93. while (!IsLoggedIn && retryCnt++ <= 3)
  94. {
  95. Login();
  96. }
  97. if (!IsLoggedIn)
  98. return false;
  99. if (!File.Exists(_addrbookFile))
  100. {
  101. StringBuilder sb = new StringBuilder("faction=checkFB");
  102. sb.Append("&wfb=main");
  103. string postdata = sb.ToString();
  104. string response = HttpHelper.GetWebPageResponse(URL_W2S_USERCONTACTS_POST, postdata,
  105. URL_W2S_USERCONTACTS_REFERRER, cookies);
  106. postdata = null;
  107. response = HttpHelper.GetWebPageResponse(URL_W2S_USERCONTACTS_GET, postdata,
  108. URL_W2S_USERCONTACTS_REFERRER, cookies);
  109. response = HttpHelper.GetWebPageResponse(URL_W2S_DASHBOARD, postdata, URL_W2S_USERCONTACTS_REFERRER,
  110. cookies);
  111. postdata = "folder=DashBoard";
  112. response = HttpHelper.GetWebPageResponse(URL_W2S_QUICKCONTACTS, postdata, URL_W2S_USERCONTACTS_REFERRER,
  113. cookies);
  114. //response = HttpHelper.GetWebPageResponse(URL_W2S_USERCONTACTS_GET, postdata, URL_W2S_USERCONTACTS_REFERRER, cookies);
  115. if (string.IsNullOrEmpty(response))
  116. return false;
  117. // Parse contacts
  118. //<input type='hidden' id='Quckvalue' name='Quckvalue' value=' '>
  119. //<input type='hidden' id='Qucktitle' name='Qucktitle' value=', '>
  120. string prefix = "<input type='hidden' id='Quckvalue' name='Quckvalue' value='*";
  121. string postfix = "'>";
  122. string names = StringParser.GetStringBetween(response, 0, prefix, postfix, null);
  123. var nameArr = names.Split('*');
  124. prefix = "<input type='hidden' id='Qucktitle' name='Qucktitle' value=',";
  125. postfix = "'>";
  126. string numbers = StringParser.GetStringBetween(response, 0, prefix, postfix, null);
  127. var numArr = numbers.Split(',');
  128. Debug.Assert(nameArr.Length == numArr.Length);
  129. int i = 0;
  130. foreach(var name in nameArr)
  131. {
  132. AddressBook.Contacts.Add(new W2SContact{Name = name, MobileNumber = numArr[i++]});
  133. }
  134. // Now write back the gd
  135. using (FileStream ws = File.Open(_addrbookFile, FileMode.Create))
  136. {
  137. BinaryFormatter bformatter = new BinaryFormatter();
  138. bformatter.Serialize(ws, AddressBook.Contacts);
  139. }
  140. return true;
  141. }
  142. using (FileStream stream = File.Open(_addrbookFile, FileMode.Open))
  143. {
  144. BinaryFormatter bformatter = new BinaryFormatter();
  145. AddressBook.Contacts = (List<W2SContact>)bformatter.Deserialize(stream);
  146. }
  147. return true;
  148. }
  149. public Way2SmsAPI(string username, string pwd)
  150. {
  151. _userName = username;
  152. _password = pwd;
  153. _addrbookFile = string.Format(AppConfig.AddressBookFile, _userName);
  154. }
  155. public void Login()
  156. {
  157. string postdata = "username=" + _userName +
  158. "&password=" + _password;
  159. string response = HttpHelper.GetWebPageResponse(URL_W2S_LOGIN, postdata, URL_W2S_LOGIN_REFERER, cookies);
  160. // do better checking (for successful login) later
  161. if (!string.IsNullOrEmpty(response))
  162. {
  163. Logger.LogIt("Way2Sms login successful");
  164. IsLoggedIn = true;
  165. }
  166. else
  167. {
  168. Logger.LogIt("Way2Sms login failed");
  169. }
  170. // Get action value
  171. response = HttpHelper.GetWebPageResponse(URL_W2S_GETACTION, null, null, cookies);
  172. string prefix = "<input type=\"hidden\" name=\"Action\" id=\"Action\" value=\"";
  173. string postfix = "\"";
  174. string action = StringParser.GetStringBetween(response, 0, prefix, postfix, null);
  175. _action = action;
  176. var cc = cookies.GetCookies(new Uri(URL_W2S_GETACTION));
  177. URL_W2S_ID = cc["JSESSIONID"].Value.Split('~')[1];
  178. URL_W2S_USERCONTACTS_REFERRER += URL_W2S_ID;
  179. }
  180. public void SendBirthdayGreeting()
  181. {
  182. }
  183. public bool SendSms(string mobileNum, string msg)
  184. {
  185. int retryCnt = 0;
  186. bool isSuccess = false;
  187. while (!IsLoggedIn && retryCnt++ <= 3)
  188. {
  189. Login();
  190. }
  191. if (!IsLoggedIn)
  192. return isSuccess;
  193. StringBuilder sb = new StringBuilder("HiddenAction=instantsms");
  194. sb.Append("&catnamedis=Birthday");
  195. sb.Append("&Action=" + _action);
  196. sb.Append("&chkall=on");
  197. sb.Append("&MobNo=" + mobileNum);
  198. sb.Append("&textArea=" + msg);
  199. string postdata = sb.ToString();
  200. //sb.Append("&");
  201. //HiddenAction instantsms
  202. //catnamedis Birthday
  203. //Action sdf5445fdg
  204. //chkall on
  205. //MobNo 9999999999
  206. //textArea helllllooo fejkeww,f fwf,we fw,g ,ggf w,f,.f'';;'er;q'q;w
  207. //guid username
  208. //gpwd *******
  209. //yuid username
  210. //ypwd *******
  211. string response = HttpHelper.GetWebPageResponse(URL_W2S_QUICKSMS, postdata, URL_W2S_QUICKSMS_REFERER, cookies);
  212. if (!string.IsNullOrEmpty(response))
  213. {
  214. isSuccess = true;
  215. }
  216. return isSuccess;
  217. }
  218. }
  219. }