How do I log into a site with WebClient?

If the problem you are having is you can authenticate but you cant keep the authentication cookie here is a cookie aware version of WebClient. private class CookieAwareWebClient : WebClient { public CookieAwareWebClient() : this(new CookieContainer()) { } public CookieAwareWebClient(CookieContainer c) { this.CookieContainer = c; } public CookieContainer CookieContainer { get; set; } protected override … Read more

How to fill forms and submit with Webclient in C#

You should probably be using HttpWebRequest for this. Here’s a simple example: var strId = UserId_TextBox.Text; var strName = Name_TextBox.Text; var encoding=new ASCIIEncoding(); var postData=”userid=”+strId; postData += (“&username=”+strName); byte[] data = encoding.GetBytes(postData); var myRequest = (HttpWebRequest)WebRequest.Create(“http://localhost/MyIdentity/Default.aspx”); myRequest.Method = “POST”; myRequest.ContentType=”application/x-www-form-urlencoded”; myRequest.ContentLength = data.Length; var newStream=myRequest.GetRequestStream(); newStream.Write(data,0,data.Length); newStream.Close(); var response = myRequest.GetResponse(); var responseStream = response.GetResponseStream(); … Read more

How to use verb GET with WebClient request?

If you use HttpWebRequest instead you would get more control of the call. You can change the REST verb by the Method property (default is GET) HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(HostURI); request.Method = “GET”; String test = String.Empty; using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) { Stream dataStream = response.GetResponseStream(); StreamReader reader = new StreamReader(dataStream); test = reader.ReadToEnd(); … Read more

HEAD with WebClient?

You are right WebClient does not support this. You can use HttpWebRequest and set the method to HEAD if you want this functionality: System.Net.WebRequest request = System.Net.WebRequest.Create(uri); request.Method = “HEAD”; request.GetResponse();

How can I change the time limit for webClient.UploadData()?

The WebClient doesn’t have a timeout property, however it is possible to inherit from the WebClient to give access to Timeout on the internal WebRequest used: public class WebClientEx : WebClient { public int Timeout {get; set;} protected override WebRequest GetWebRequest(Uri address) { var request = base.GetWebRequest(address); request.Timeout = Timeout; return request; } } Usage: … Read more

Sending HTTP POST with System.Net.WebClient

Based on @carlosfigueira ‘s answer, I looked further into WebClient’s methods and found UploadValues, which is exactly what I want: Using client As New Net.WebClient Dim reqparm As New Specialized.NameValueCollection reqparm.Add(“param1”, “somevalue”) reqparm.Add(“param2”, “othervalue”) Dim responsebytes = client.UploadValues(someurl, “POST”, reqparm) Dim responsebody = (New Text.UTF8Encoding).GetString(responsebytes) End Using The key part is this: client.UploadValues(someurl, “POST”, reqparm) … Read more