Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public void saveFile(String url)
- {
- Uri Url = new Uri(@url);
- String filePath = "c:\";
- fileName = url.Substring(url.LastIndexOf("/") + 1).Replace("%20", " ").Replace("%28", " ").Replace("%29", " ");
- SaveFileDialog saveFileDialog1 = new SaveFileDialog();
- saveFileDialog1.FileName = fileName;
- if (saveFileDialog1.ShowDialog() == DialogResult.OK)
- {
- filePath = saveFileDialog1.FileName;
- CookieAwareWebClient http = new CookieAwareWebClient(new CookieContainer());
- http.DownloadProgressChanged += new DownloadProgressChangedEventHandler(client_DownloadProgressChanged);
- http.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";
- string response = http.UploadString("http://courseweb.sliit.lk/login/index.php", "username=" + Form1.USERNAME + "&password=" + Form1.PASSWORD + "&submit=submit");
- //http.DownloadFile(Url, filePath);
- http.DownloadFileAsync(Url, filePath);
- this.Text = "Downloading File - " + fileName;
- }
- }
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Net;
- using System.Text;
- namespace Course_Web
- {
- public class CookieAwareWebClient : WebClient
- {
- Uri target = new Uri("http://unversitywebsite.com");
- public CookieContainer CookieContainer { get; set; }
- public Uri Uri { get; set; }
- public CookieAwareWebClient()
- : this(new CookieContainer())
- {
- }
- public CookieAwareWebClient(CookieContainer cookies)
- {
- this.CookieContainer = cookies;
- }
- protected override WebRequest GetWebRequest(Uri address)
- {
- WebRequest request = base.GetWebRequest(address);
- if (request is HttpWebRequest)
- {
- (request as HttpWebRequest).CookieContainer = this.CookieContainer;
- }
- HttpWebRequest httpRequest = (HttpWebRequest)request;
- httpRequest.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
- return httpRequest;
- }
- protected override WebResponse GetWebResponse(WebRequest request)
- {
- WebResponse response = base.GetWebResponse(request);
- String setCookieHeader = response.Headers[HttpResponseHeader.SetCookie];
- if (setCookieHeader != null)
- {
- //do something if needed to parse out the cookie.
- if (setCookieHeader != null)
- {
- Cookie cookie = new Cookie("CookieName", "CookieValue") { Domain = target.Host }; //create cookie
- this.CookieContainer.Add(cookie);
- }
- }
- return response;
- }
- }
- }
Add Comment
Please, Sign In to add comment