RegistryController.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. using Microsoft.AspNetCore.Mvc;
  2. using SuperCms.Models;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. using System.Net;
  7. using System.Net.Http;
  8. using System.Text.RegularExpressions;
  9. using System.Threading.Tasks;
  10. using SuperCms.Controllers;
  11. using Syncfusion.XlsIO;
  12. using System.Text;
  13. namespace SuperCms.Controllers
  14. {
  15. public class RegistryController : BaseController
  16. {
  17. public async Task<IActionResult> IndexAsync(string txtThoigian, string txtLoaiPT, string cboTinh)
  18. {
  19. try
  20. {
  21. RegistryViewModel model = new RegistryViewModel();
  22. if (txtThoigian == null || txtLoaiPT == null || cboTinh == null)
  23. {
  24. return View(model);
  25. }
  26. string endPoint = "http://app.vr.org.vn/ptpublic/quahankd.aspx";
  27. var client = new HttpClient();
  28. // get valid key
  29. WebRequest request = HttpWebRequest.Create(endPoint);
  30. WebResponse response1 = request.GetResponse();
  31. StreamReader reader = new StreamReader(response1.GetResponseStream());
  32. string responseText = reader.ReadToEnd();
  33. bool loop = true;
  34. int i = 0;
  35. var tableHTML = "";
  36. var table = "";
  37. string old = "";
  38. List<KeyValuePair<string, string>> data = new List<KeyValuePair<string, string>>();
  39. List<KeyValuePair<string, string>> dataOld = new List<KeyValuePair<string, string>>();
  40. while (loop)
  41. {
  42. //if (i == 0)
  43. //{
  44. // data.Add(new KeyValuePair<string, string>("__EVENTTARGET", ""));
  45. // data.Add(new KeyValuePair<string, string>("__EVENTARGUMENT", ""));
  46. // data.Add(new KeyValuePair<string, string>("cmdTraCuu", "Tra cứu"));
  47. //}
  48. //else
  49. //{
  50. // data.Add(new KeyValuePair<string, string>("__EVENTTARGET", "DGTimPT$ctl54$ctl" + (i < 10 ? "0" + i.ToString() : i.ToString())));
  51. // data.Add(new KeyValuePair<string, string>("__EVENTARGUMENT", ""));
  52. //}
  53. using (var httpClient = new HttpClient())
  54. {
  55. if (i == 0)
  56. {
  57. List<KeyValuePair<string, string>> dataGet = UtilsController.ParseDataRequest(responseText);
  58. dataGet.Add(new KeyValuePair<string, string>("cboTinh", cboTinh));
  59. dataGet.Add(new KeyValuePair<string, string>("txtLoaiPT", txtLoaiPT));
  60. dataGet.Add(new KeyValuePair<string, string>("txtThoigian", txtThoigian));
  61. dataGet.Add(new KeyValuePair<string, string>("__EVENTTARGET", ""));
  62. dataGet.Add(new KeyValuePair<string, string>("__EVENTARGUMENT", ""));
  63. dataGet.Add(new KeyValuePair<string, string>("cmdTraCuu", "Tra cứu"));
  64. data = dataGet;
  65. }
  66. else
  67. {
  68. dataOld.Add(new KeyValuePair<string, string>("__EVENTTARGET", "DGTimPT$ctl54$ctl" + (i < 10 ? "0" + i.ToString() : i.ToString())));
  69. dataOld.Add(new KeyValuePair<string, string>("__EVENTARGUMENT", ""));
  70. dataOld.Add(new KeyValuePair<string, string>("cboTinh", cboTinh));
  71. dataOld.Add(new KeyValuePair<string, string>("txtLoaiPT", txtLoaiPT));
  72. dataOld.Add(new KeyValuePair<string, string>("txtThoigian", txtThoigian));
  73. data = dataOld;
  74. }
  75. using (var content = new FormUrlEncodedContent(data))
  76. {
  77. content.Headers.Clear();
  78. content.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
  79. HttpResponseMessage response = await httpClient.PostAsync(endPoint, content);
  80. var result = response.Content.ReadAsStringAsync().Result;
  81. dataOld = UtilsController.ParseDataRequest(result);
  82. if (old != null)
  83. {
  84. if (old.Length == result.Length)
  85. {
  86. loop = false;
  87. continue;
  88. }
  89. }
  90. old = result;
  91. Regex regex1 = new Regex("<table cell[^>]*>[\\s\\S]*?<\\/table>", RegexOptions.IgnoreCase);
  92. Match match1;
  93. for (match1 = regex1.Match(result); match1.Success; match1 = match1.NextMatch())
  94. {
  95. var result2 = match1.Groups[0].Value;
  96. Regex regex2 = new Regex("<tr[^>]*>[\\s\\S]*?<\\/tr>", RegexOptions.IgnoreCase);
  97. Match match2;
  98. int count = 0;
  99. for (match2 = regex2.Match(result2); match2.Success; match2 = match2.NextMatch())
  100. {
  101. if (count > 0)
  102. {
  103. if (!match2.Groups[0].Value.Contains("DGTimPT$ctl"))
  104. {
  105. var d = match2.Groups[0].Value;
  106. table = table + d;
  107. }
  108. }
  109. count++;
  110. }
  111. }
  112. }
  113. }
  114. i++;
  115. }
  116. //tableHTML = "<html>" + "<body>" +
  117. // "<table cellspacing=\"0\" width=\"500\" style=\"table-layout:fixed; border-collapse:collapse; width: 500pt\">" +
  118. // @"<tr><td> Số TT </td>
  119. // <td> Biển đăng ký</td>
  120. // <td> Ngày đăng ký</td>
  121. // <td> Nhãn hiệu </td>
  122. // <td> Chủ phương tiên</td>
  123. // <td> Địa chỉ </td>
  124. // <td> Ngày kiểm định</td>
  125. // <td> Đơn vị KĐ</td>
  126. // <td> Số GCN KĐ</td>
  127. // <td> Thời hạn GCN</td>
  128. // </tr>" +
  129. // table +
  130. // @"</table>" +
  131. // "</body></html>";
  132. //String tableHTMLNew = tableHTML.Replace("&", "&amp;");
  133. //MemoryStream ms = new MemoryStream();
  134. //using (ExcelEngine excelEngine = new ExcelEngine())
  135. //{
  136. // // Step 2 : Instantiate the Excel application object.
  137. // IApplication application = excelEngine.Excel;
  138. // application.DefaultVersion = ExcelVersion.Excel2016;
  139. // // A workbook is created.
  140. // IWorkbook workbook = application.Workbooks.Create(1);
  141. // // The first worksheet object in the worksheets collection is accessed.
  142. // IWorksheet worksheet = workbook.Worksheets[0];
  143. // byte[] byteArray = Encoding.UTF8.GetBytes(tableHTMLNew);
  144. // MemoryStream file = new MemoryStream(byteArray);
  145. // // Imports HTML table into the worksheet from first row and first column.
  146. // worksheet.ImportHtmlTable(file, 1, 1);
  147. // worksheet.UsedRange.AutofitColumns();
  148. // worksheet.UsedRange.AutofitRows();
  149. // workbook.SaveAs(ms);
  150. // ms.Position = 0;
  151. //}
  152. //return File(ms, "Application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "Export-HTML-Table.xlsx");
  153. model.table = table;
  154. return View(model);
  155. }
  156. catch (Exception ex)
  157. {
  158. return Redirect("/Home");
  159. }
  160. }
  161. public async Task<IActionResult> GetDataAsync(string txtThoigian, string txtLoaiPT, string cboTinh)
  162. {
  163. try
  164. {
  165. string endPoint = "http://app.vr.org.vn/ptpublic/quahankd.aspx";
  166. var client = new HttpClient();
  167. // get valid key
  168. WebRequest request = HttpWebRequest.Create(endPoint);
  169. WebResponse response1 = request.GetResponse();
  170. StreamReader reader = new StreamReader(response1.GetResponseStream());
  171. string responseText = reader.ReadToEnd();
  172. bool loop = true;
  173. int i = 0;
  174. var tableHTML = "";
  175. var table = "";
  176. string old = "";
  177. List<KeyValuePair<string, string>> data = new List<KeyValuePair<string, string>>();
  178. List<KeyValuePair<string, string>> dataOld = new List<KeyValuePair<string, string>>();
  179. while (loop)
  180. {
  181. //if (i == 0)
  182. //{
  183. // data.Add(new KeyValuePair<string, string>("__EVENTTARGET", ""));
  184. // data.Add(new KeyValuePair<string, string>("__EVENTARGUMENT", ""));
  185. // data.Add(new KeyValuePair<string, string>("cmdTraCuu", "Tra cứu"));
  186. //}
  187. //else
  188. //{
  189. // data.Add(new KeyValuePair<string, string>("__EVENTTARGET", "DGTimPT$ctl54$ctl" + (i < 10 ? "0" + i.ToString() : i.ToString())));
  190. // data.Add(new KeyValuePair<string, string>("__EVENTARGUMENT", ""));
  191. //}
  192. using (var httpClient = new HttpClient())
  193. {
  194. if (i == 0)
  195. {
  196. List<KeyValuePair<string, string>> dataGet = UtilsController.ParseDataRequest(responseText);
  197. dataGet.Add(new KeyValuePair<string, string>("cboTinh", cboTinh));
  198. dataGet.Add(new KeyValuePair<string, string>("txtLoaiPT", txtLoaiPT));
  199. dataGet.Add(new KeyValuePair<string, string>("txtThoigian", txtThoigian));
  200. dataGet.Add(new KeyValuePair<string, string>("__EVENTTARGET", ""));
  201. dataGet.Add(new KeyValuePair<string, string>("__EVENTARGUMENT", ""));
  202. dataGet.Add(new KeyValuePair<string, string>("cmdTraCuu", "Tra cứu"));
  203. data = dataGet;
  204. }
  205. else
  206. {
  207. dataOld.Add(new KeyValuePair<string, string>("__EVENTTARGET", "DGTimPT$ctl54$ctl" + (i < 10 ? "0" + i.ToString() : i.ToString())));
  208. dataOld.Add(new KeyValuePair<string, string>("__EVENTARGUMENT", ""));
  209. dataOld.Add(new KeyValuePair<string, string>("cboTinh", cboTinh));
  210. dataOld.Add(new KeyValuePair<string, string>("txtLoaiPT", txtLoaiPT));
  211. dataOld.Add(new KeyValuePair<string, string>("txtThoigian", txtThoigian));
  212. data = dataOld;
  213. }
  214. using (var content = new FormUrlEncodedContent(data))
  215. {
  216. content.Headers.Clear();
  217. content.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
  218. HttpResponseMessage response = await httpClient.PostAsync(endPoint, content);
  219. var result = response.Content.ReadAsStringAsync().Result;
  220. dataOld = UtilsController.ParseDataRequest(result);
  221. if (old != null)
  222. {
  223. if (old.Length == result.Length)
  224. {
  225. loop = false;
  226. continue;
  227. }
  228. }
  229. old = result;
  230. Regex regex1 = new Regex("<table cell[^>]*>[\\s\\S]*?<\\/table>", RegexOptions.IgnoreCase);
  231. Match match1;
  232. for (match1 = regex1.Match(result); match1.Success; match1 = match1.NextMatch())
  233. {
  234. var result2 = match1.Groups[0].Value;
  235. Regex regex2 = new Regex("<tr[^>]*>[\\s\\S]*?<\\/tr>", RegexOptions.IgnoreCase);
  236. Match match2;
  237. int count = 0;
  238. for (match2 = regex2.Match(result2); match2.Success; match2 = match2.NextMatch())
  239. {
  240. if (count > 0)
  241. {
  242. if (!match2.Groups[0].Value.Contains("DGTimPT$ctl"))
  243. {
  244. var d = match2.Groups[0].Value;
  245. table = table + d;
  246. }
  247. }
  248. count++;
  249. }
  250. }
  251. }
  252. }
  253. i++;
  254. }
  255. //tableHTML = "<html>" + "<body>" +
  256. // "<table cellspacing=\"0\" width=\"500\" style=\"table-layout:fixed; border-collapse:collapse; width: 500pt\">" +
  257. // @"<tr><td> Số TT </td>
  258. // <td> Biển đăng ký</td>
  259. // <td> Ngày đăng ký</td>
  260. // <td> Nhãn hiệu </td>
  261. // <td> Chủ phương tiên</td>
  262. // <td> Địa chỉ </td>
  263. // <td> Ngày kiểm định</td>
  264. // <td> Đơn vị KĐ</td>
  265. // <td> Số GCN KĐ</td>
  266. // <td> Thời hạn GCN</td>
  267. // </tr>" +
  268. // table +
  269. // @"</table>" +
  270. // "</body></html>";
  271. //String tableHTMLNew = tableHTML.Replace("&", "&amp;");
  272. //MemoryStream ms = new MemoryStream();
  273. //using (ExcelEngine excelEngine = new ExcelEngine())
  274. //{
  275. // // Step 2 : Instantiate the Excel application object.
  276. // IApplication application = excelEngine.Excel;
  277. // application.DefaultVersion = ExcelVersion.Excel2016;
  278. // // A workbook is created.
  279. // IWorkbook workbook = application.Workbooks.Create(1);
  280. // // The first worksheet object in the worksheets collection is accessed.
  281. // IWorksheet worksheet = workbook.Worksheets[0];
  282. // byte[] byteArray = Encoding.UTF8.GetBytes(tableHTMLNew);
  283. // MemoryStream file = new MemoryStream(byteArray);
  284. // // Imports HTML table into the worksheet from first row and first column.
  285. // worksheet.ImportHtmlTable(file, 1, 1);
  286. // worksheet.UsedRange.AutofitColumns();
  287. // worksheet.UsedRange.AutofitRows();
  288. // workbook.SaveAs(ms);
  289. // ms.Position = 0;
  290. //}
  291. //return File(ms, "Application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "Export-HTML-Table.xlsx");
  292. RegistryViewModel model = new RegistryViewModel();
  293. model.table = tableHTML;
  294. return RedirectToAction("/Index", model);
  295. }
  296. catch (Exception ex)
  297. {
  298. return Redirect("/Home");
  299. }
  300. }
  301. }
  302. }