How to Replace WebClient with HttpClient?
// Injecting HttpClient would be a better idea if possible HttpClient client = new(); string page = await client.GetStringAsync(“page URL here”);
// Injecting HttpClient would be a better idea if possible HttpClient client = new(); string page = await client.GetStringAsync(“page URL here”);
You’re not setting the “Content-Type” request header, so you need to append .contentType(MediaType.APPLICATION_JSON) to the request building part.
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
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
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
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();
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
The following works for me: client.UploadValues(url, “DELETE”, new NameValueCollection());
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