Using CookieContainer with WebClient class

WebClient wb = new WebClient(); wb.Headers.Add(HttpRequestHeader.Cookie, “somecookie”); From Comments How do you format the name and value of the cookie in place of “somecookie” ? wb.Headers.Add(HttpRequestHeader.Cookie, “cookiename=cookievalue”); For multiple cookies: wb.Headers.Add(HttpRequestHeader.Cookie, “cookiename1=cookievalue1;” + “cookiename2=cookievalue2”);

Upload and download a file to/from FTP server in C#/.NET

Upload The most trivial way to upload a binary file to an FTP server using .NET framework is using WebClient.UploadFile: WebClient client = new WebClient(); client.Credentials = new NetworkCredential(“username”, “password”); client.UploadFile( “ftp://ftp.example.com/remote/path/file.zip”, @”C:\local\path\file.zip”); If you need a greater control, that WebClient does not offer (like TLS/SSL encryption, ascii/text transfer mode, transfer resuming, etc), use FtpWebRequest. … Read more

Set timeout for webClient.DownloadFile()

My answer comes from here You can make a derived class, which will set the timeout property of the base WebRequest class: using System; using System.Net; public class WebDownload : WebClient { /// <summary> /// Time in milliseconds /// </summary> public int Timeout { get; set; } public WebDownload() : this(60000) { } public WebDownload(int … Read more

Login to website, via C#

You can continue using WebClient to POST (instead of GET, which is the HTTP verb you’re currently using with DownloadString), but I think you’ll find it easier to work with the (slightly) lower-level classes WebRequest and WebResponse. There are two parts to this – the first is to post the login form, the second is … Read more

Automatically decompress gzip response via WebClient.DownloadData

WebClient uses HttpWebRequest under the covers. And HttpWebRequest supports gzip/deflate decompression. See HttpWebRequest AutomaticDecompression property However, WebClient class does not expose this property directly. So you will have to derive from it to set the property on the underlying HttpWebRequest. class MyWebClient : WebClient { protected override WebRequest GetWebRequest(Uri address) { HttpWebRequest request = base.GetWebRequest(address) … Read more

How to post data to specific URL using WebClient in C#

I just found the solution and yea it was easier than I thought 🙂 so here is the solution: string URI = “http://www.myurl.com/post.php”; string myParameters = “param1=value1&param2=value2&param3=value3”; using (WebClient wc = new WebClient()) { wc.Headers[HttpRequestHeader.ContentType] = “application/x-www-form-urlencoded”; string HtmlResult = wc.UploadString(URI, myParameters); } it works like charm 🙂