using System.Net; using System.IO; HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://jsonplaceholder.typicode.com/posts/1"); request.Method = "GET"; HttpWebResponse response = (HttpWebResponse)request.GetResponse(); Stream dataStream = response.GetResponseStream(); StreamReader reader = new StreamReader(dataStream); string responseFromServer = reader.ReadToEnd(); Console.WriteLine(responseFromServer);
using System.Net; using System.IO; using System.Text; string postData = "username=myusername&password=mypassword"; byte[] data = Encoding.UTF8.GetBytes(postData); HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://example.com/login"); request.Method = "POST"; request.ContentType = "application/x-www-form-urlencoded"; request.ContentLength = data.Length; using (Stream stream = request.GetRequestStream()) { stream.Write(data, 0, data.Length); } HttpWebResponse response = (HttpWebResponse)request.GetResponse(); Stream responseStream = response.GetResponseStream(); StreamReader reader = new StreamReader(responseStream); string responseFromServer = reader.ReadToEnd(); Console.WriteLine(responseFromServer);This example uses the HttpWebRequest class to send a POST request to "https://example.com/login" with some data and receives the response back as a string. Package/Library: System.Net.HttpWebRequest is part of the .NET Framework Class Library. No external package/library need to be installed separately, it is part of the core framework.