Guest User

Untitled

a guest
Feb 18th, 2019
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.79 KB | None | 0 0
  1. public void saveFile(String url)
  2. {
  3. Uri Url = new Uri(@url);
  4. String filePath = "c:\";
  5. fileName = url.Substring(url.LastIndexOf("/") + 1).Replace("%20", " ").Replace("%28", " ").Replace("%29", " ");
  6. SaveFileDialog saveFileDialog1 = new SaveFileDialog();
  7. saveFileDialog1.FileName = fileName;
  8. if (saveFileDialog1.ShowDialog() == DialogResult.OK)
  9. {
  10. filePath = saveFileDialog1.FileName;
  11. CookieAwareWebClient http = new CookieAwareWebClient(new CookieContainer());
  12. http.DownloadProgressChanged += new DownloadProgressChangedEventHandler(client_DownloadProgressChanged);
  13. http.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";
  14. string response = http.UploadString("http://courseweb.sliit.lk/login/index.php", "username=" + Form1.USERNAME + "&password=" + Form1.PASSWORD + "&submit=submit");
  15. //http.DownloadFile(Url, filePath);
  16. http.DownloadFileAsync(Url, filePath);
  17. this.Text = "Downloading File - " + fileName;
  18. }
  19. }
  20.  
  21. using System;
  22. using System.Collections.Generic;
  23. using System.Linq;
  24. using System.Net;
  25. using System.Text;
  26.  
  27. namespace Course_Web
  28. {
  29. public class CookieAwareWebClient : WebClient
  30. {
  31. Uri target = new Uri("http://unversitywebsite.com");
  32. public CookieContainer CookieContainer { get; set; }
  33. public Uri Uri { get; set; }
  34.  
  35.  
  36.  
  37. public CookieAwareWebClient()
  38. : this(new CookieContainer())
  39. {
  40. }
  41.  
  42. public CookieAwareWebClient(CookieContainer cookies)
  43. {
  44. this.CookieContainer = cookies;
  45. }
  46.  
  47. protected override WebRequest GetWebRequest(Uri address)
  48. {
  49. WebRequest request = base.GetWebRequest(address);
  50. if (request is HttpWebRequest)
  51. {
  52. (request as HttpWebRequest).CookieContainer = this.CookieContainer;
  53. }
  54. HttpWebRequest httpRequest = (HttpWebRequest)request;
  55. httpRequest.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
  56. return httpRequest;
  57. }
  58.  
  59. protected override WebResponse GetWebResponse(WebRequest request)
  60. {
  61. WebResponse response = base.GetWebResponse(request);
  62. String setCookieHeader = response.Headers[HttpResponseHeader.SetCookie];
  63.  
  64. if (setCookieHeader != null)
  65. {
  66. //do something if needed to parse out the cookie.
  67. if (setCookieHeader != null)
  68. {
  69. Cookie cookie = new Cookie("CookieName", "CookieValue") { Domain = target.Host }; //create cookie
  70. this.CookieContainer.Add(cookie);
  71. }
  72. }
  73. return response;
  74. }
  75. }
  76. }
Add Comment
Please, Sign In to add comment