Advertisement
Guest User

Test case 3. Junior C# Developer

a guest
Mar 3rd, 2019
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.14 KB | None | 0 0
  1. using HtmlAgilityPack;
  2. using System;
  3. using System.Collections.Specialized;
  4. using System.IO;
  5. using System.Net;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8.  
  9. namespace SemanticForce.TestProject2
  10. {
  11. class Program
  12. {
  13. static void Main(string[] args)
  14. {
  15. Console.OutputEncoding = CodePagesEncodingProvider.Instance.GetEncoding(1251);
  16. const string baseUrl = "https://forum.0day.kiev.ua";
  17. const string loginUrl = "https://forum.0day.kiev.ua/index.php?act=Login&CODE=01&CookieDate=1";
  18.  
  19. //step 1 get cookie
  20. HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(baseUrl);
  21. //webRequest.Proxy = new WebProxy("127.0.0.1", 8888);
  22. webRequest.UserAgent = "Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.80 Safari/5";
  23. webRequest.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*";
  24. webRequest.Headers.Add("accept-language","uk");
  25. webRequest.ContentType = "application/x-www-form-urlencoded";
  26. HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse();
  27. string sCookies = String.IsNullOrEmpty(webResponse.Headers["Set-Cookie"])?
  28. "":webResponse.Headers["Set-Cookie"];
  29.  
  30. //step 2 send post request for authorization
  31. webRequest = (HttpWebRequest)WebRequest.Create(loginUrl);
  32. webRequest.Method = "POST";
  33. webRequest.Referer = baseUrl;
  34. webRequest.UserAgent = "Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.80 Safari/5";
  35. webRequest.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*";
  36. webRequest.Headers.Add("accept-language", "uk");
  37. webRequest.ContentType = "application/x-www-form-urlencoded";
  38. webRequest.Headers.Add(HttpRequestHeader.Cookie, sCookies);
  39. webRequest.AllowAutoRedirect = false;
  40. string sQueryString = "UserName=DarkWarrior&PassWord=mSGes4i7yG3XRzU";
  41. byte[] byteArr = CodePagesEncodingProvider.Instance.GetEncoding(1251).GetBytes(sQueryString);
  42. webRequest.ContentLength = byteArr.Length;
  43. webRequest.GetRequestStream().Write(byteArr, 0, byteArr.Length);
  44. try
  45. {
  46. webResponse = (HttpWebResponse)webRequest.GetResponse();
  47. }
  48. catch (WebException e) {
  49. if (e.Message.Contains("302"))
  50. webResponse = (HttpWebResponse)e.Response;
  51. }
  52.  
  53. //step 3 load data
  54. string sLocation = webResponse.Headers["Location"];
  55. sCookies = "";
  56. if (!String.IsNullOrEmpty(webResponse.Headers["Set-Cookie"]))
  57. {
  58. sCookies = webResponse.Headers["Set-Cookie"];
  59. }
  60. webRequest = (HttpWebRequest)HttpWebRequest.Create(loginUrl);
  61. webRequest.Referer = loginUrl;
  62. webRequest.UserAgent = "Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.80 Safari/5";
  63. webRequest.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*";
  64. webRequest.Headers.Add("accept-language", "uk");
  65. webRequest.ContentType = "application/x-www-form-urlencoded";
  66. if (!String.IsNullOrEmpty(sCookies))
  67. {
  68. webRequest.Headers.Add(HttpRequestHeader.Cookie, sCookies);
  69. }
  70. webResponse = (HttpWebResponse)webRequest.GetResponse();
  71. StreamReader myStreamReader = new StreamReader(webResponse.GetResponseStream(), CodePagesEncodingProvider.Instance.GetEncoding(1251));
  72. HtmlDocument htmlDoc = new HtmlDocument();
  73. htmlDoc.LoadHtml(myStreamReader.ReadToEnd());
  74. var data = htmlDoc.DocumentNode.SelectSingleNode("//div[@id='userlinks']");
  75. Console.WriteLine(data.InnerHtml);
  76. Console.ReadKey();
  77. }
  78. }
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement