This tutorial shows a simple example on How to send data (with POST or GET method) to a remote web server via its URL.
There are various implement ways and purpose usages why you want to send something to a server or get information from that. I don’t want to mention about that in the scope of the post, just want to give an example about HttpWebRequest and HttpWebResponse (in System.NET namespace) to make request and get response from remote server.
using System; using System.IO; using System.Collections.Generic; using System.Net; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; public partial class http_request : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { string http_url = "http://4rapiddev.com"; Uri uri = new Uri(http_url); string data = "key1=value1&key2=value2"; if (uri.Scheme == Uri.UriSchemeHttp) { HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(uri); request.Method = WebRequestMethods.Http.Post; request.ContentLength = data.Length; request.ContentType = "application/x-www-form-urlencoded"; StreamWriter writer = new StreamWriter(request.GetRequestStream()); writer.Write(data); writer.Close(); HttpWebResponse response = (HttpWebResponse)request.GetResponse(); StreamReader reader = new StreamReader(response.GetResponseStream()); string output = reader.ReadToEnd(); response.Close(); Response.Write(output); } } } |
The example above is using POST method to send request (line 19), if you want to use GET method instead; simply replace Post with Get (WebRequestMethods.Http.Get).