Search This Blog

Thursday, February 7, 2013

Call REST (C#)

C# > WebRequestMethods.Http.Get

WebRequestMethods.Http.Get: Represents an HTTP GET protocol method.

The GET method retrieves the information or entity that is identified by the URI of the request.

Example:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication3
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            string Uri = "Your Uri";
            String text;
            HttpWebRequest myReq = (HttpWebRequest)WebRequest.Create(Uri);
            myReq.Method = WebRequestMethods.Http.Get;
            myReq.Accept = "application/json";

            HttpWebResponse response = (HttpWebResponse)myReq.GetResponse();
            using (var sr = new StreamReader(response.GetResponseStream()))
            {
                text = sr.ReadToEnd();
            }
        }
    }
}