HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://www.example.com"); request.Method = "GET"; request.BeginGetResponse(result => { HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(result); StreamReader reader = new StreamReader(response.GetResponseStream()); string responseText = reader.ReadToEnd(); Console.WriteLine(responseText); }, null);
Uri address = new Uri("http://www.example.com"); WebRequest request = WebRequest.Create(address); request.Method = "POST"; request.ContentType = "application/x-www-form-urlencoded"; string postData = "param1=value1¶m2=value2"; byte[] postDataBytes = Encoding.UTF8.GetBytes(postData); request.BeginGetRequestStream(result => { Stream postStream = request.EndGetRequestStream(result); postStream.Write(postDataBytes, 0, postDataBytes.Length); postStream.Close(); request.BeginGetResponse(responseResult => { HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(responseResult); StreamReader reader = new StreamReader(response.GetResponseStream()); string responseText = reader.ReadToEnd(); Console.WriteLine(responseText); }, null); }, null);This example shows how to make a POST request with data and print out the response to the console. It uses the request stream to write the data to the request before sending it. The package library for BeginGetResponse is the System.Net namespace in the .NET Framework.