HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://www.example.com"); request.Method = "POST"; request.ContentType = "application/x-www-form-urlencoded"; request.BeginGetRequestStream((ar) => { Stream stream = request.EndGetRequestStream(ar); string data = "name=value"; byte[] buffer = Encoding.UTF8.GetBytes(data); stream.Write(buffer, 0, buffer.Length); stream.Close(); request.BeginGetResponse((ar2) => { HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(ar2); using (StreamReader reader = new StreamReader(response.GetResponseStream())) { string content = reader.ReadToEnd(); Console.WriteLine(content); } }, null); }, null);
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) { using (Stream stream = response.GetResponseStream()) { byte[] buffer = new byte[1024]; int bytesRead = 0; while ((bytesRead = stream.Read(buffer, 0, buffer.Length)) > 0) { Console.Write(Encoding.UTF8.GetString(buffer, 0, bytesRead)); } } }This example sends a GET request to the specified URL and reads the response in a loop. It uses the synchronous GetResponse method instead of the async methods. Package library: System.Net