Advertisement
Guest User

Untitled

a guest
Feb 10th, 2017
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.45 KB | None | 0 0
  1. static void getResponse(string loginUrl, string secondUrl, string username, string password, string cookieName = null)
  2. {
  3. HttpWebRequest request = (HttpWebRequest)WebRequest.Create(loginUrl);
  4. request.Method = "POST";
  5.  
  6. CookieContainer container = new CookieContainer();
  7.  
  8. if (cookieName != null)
  9. {
  10. container.Add(new Cookie(cookieName, username, "/", new Uri(loginUrl).Host));
  11. }
  12.  
  13. string postData = String.Format("Username={0}&Password={1}", username, password);
  14.  
  15. ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(AcceptAllCertifications);
  16.  
  17. byte[] byteArray = Encoding.UTF8.GetBytes(postData);
  18.  
  19. request.ContentType = "application/x-www-form-urlencoded";
  20. request.ContentLength = byteArray.Length;
  21.  
  22. Stream dataStream = request.GetRequestStream();
  23. dataStream.Write(byteArray, 0, byteArray.Length);
  24. dataStream.Close();
  25.  
  26. WebResponse response = request.GetResponse();
  27. dataStream = response.GetResponseStream();
  28.  
  29. //Need to somehow find the name of the inputs for username and password, as I think I
  30. //currently have them incorrect.
  31.  
  32. StreamReader reader = new StreamReader(dataStream);
  33. string responseFromServer = reader.ReadToEnd();
  34.  
  35. using (StreamWriter outfile = new StreamWriter(@"C:\Users\jacko\Documents\tempFile1.xml"))
  36. {
  37. outfile.Write(responseFromServer.ToString());
  38. }
  39.  
  40. reader.Close();
  41. dataStream.Close();
  42. response.Close();
  43.  
  44. request = (HttpWebRequest)WebRequest.Create(secondUrl);
  45. request.CookieContainer = container;
  46.  
  47. response = request.GetResponse();
  48.  
  49. dataStream = response.GetResponseStream();
  50. reader = new StreamReader(dataStream);
  51. responseFromServer = reader.ReadToEnd();
  52.  
  53. using (StreamWriter outfile = new StreamWriter(@"C:\Users\jacko\Documents\tempFile.xml"))
  54. {
  55. outfile.Write(responseFromServer.ToString());
  56. }
  57.  
  58. reader.Close();
  59. dataStream.Close();
  60. response.Close();
  61. }
  62.  
  63. public static bool AcceptAllCertifications(object sender,
  64. System.Security.Cryptography.X509Certificates.X509Certificate certification,
  65. System.Security.Cryptography.X509Certificates.X509Chain chain,
  66. System.Net.Security.SslPolicyErrors sslPolicyErrors)
  67. {
  68. return true;
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement