Truemag

  • Categories
    • Tips And Tricks
    • Internet
    • PHP
    • Javascript
    • CSharp
    • SQL Server
    • Linux
  • Lastest Videos
  • Our Demos
  • About
  • Contact
  • Home
  • Write With Us
  • Job Request
Home CSharp ASP.NET Web Request Post/Get HTTP Example

ASP.NET Web Request Post/Get HTTP Example

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);
        }
    }
}

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).

Sep 21, 2011Hoan Huynh
Asp.net Load Connection String Dynamically For Different EnvironmentsASP.NET Web Request POST/GET HTTPS Ignore Certificate Validation
You Might Also Like:
  • ASP.NET Web Request POST/GET HTTPS Ignore Certificate Validation
  • ASP.Net C# Download Or Save Image File From URL
  • ASP.NET Get Facebook URL Total Comments, Total Likes, Total Shares
  • PHP CURL Post To HTTPS Website
  • ASP.NET Cookie Domains HTTP HTTPS
  • C# Read File Content
  • Facebook Publish To Wall With External Link And Track Callback
  • JavaScript Detect Protocol (HTTP/ HTTPS) To Load Or Handle Accordingly
  • C# Check Google PageRank – ASP.NET Example
  • ASP.NET JQuery Autocomplete Textbox
Hoan Huynh

Hoan Huynh is the founder and head of 4rapiddev.com. Reach him at [email protected]

10 years ago CSharpGetResponseStream, HttpWebRequest, HttpWebResponse, StreamReader531
0
GooglePlus
0
Facebook
0
Twitter
0
Digg
0
Delicious
0
Stumbleupon
0
Linkedin
0
Pinterest
Most Viewed
PHP Download Image Or File From URL
24,550 views
Notepad Plus Plus Compare Plugin
How To Install Compare Text Plugin In Notepad Plus Plus
21,886 views
Microsoft SQL Server 2008 Attach Remove Log
Delete, Shrink, Eliminate Transaction Log .LDF File
17,740 views
JQuery Allow only numeric characters or only alphabet characters in textbox
15,063 views
C# Read Json From URL And Parse/Deserialize Json
11,795 views
4 Rapid Development is a central page that is targeted at newbie and professional programmers, database administrators, system admin, web masters and bloggers.
Recent Posts
  • Things to Learn about Installingderm Loan Type S
  • Online Photo Editor – Free Photoediting Software
  • A Guide to Finding the Best Paper Sellers
  • Photoediting in Home Isn’t Hard to Do!

  • Free Photo Editor Online
Categories
  • CSharp (45)
  • Facebook Graph API (19)
  • Google API (7)
  • Internet (87)
  • iPhone XCode (8)
  • Javascript (35)
  • Linux (27)
  • MySQL (16)
  • PHP (84)
  • Problem Issue Error (29)
  • Resources (32)
  • SQL Server (25)
  • Timeline (5)
  • Tips And Tricks (141)
  • Uncategorized (647)
Recommended
  • Custom Software Development Company
  • Online Useful Tools
  • Premium Themes
  • VPS
2014 © 4 Rapid Development