Oops! It appears that you have disabled your Javascript. In order for you to see this page as it is meant to appear, we ask that you please re-enable your Javascript!

Basic REST Api – Client Request Data From Server

Below is the example of a client getting data from server through REST Api. Gzip encode header was used in this example, therefore the stream received on the client side will have to decode back. Content type pass from server will be application/json.

 public static void Main(string[] args)
        {
            try
            {
                var req = Create("http://www.abc.com/api/inboundtest/Inbound", "POST");
                var w = Base64Encode("username:password");
                req.Headers.Add("Authorization", "Basic " + w);
                var res = req.GetResponse();
                using (Stream ReceiveStream = res.GetResponseStream())
                using (var gzipStream = new GZipStream(ReceiveStream, CompressionMode.Decompress))
                using (StreamReader reader = new StreamReader(gzipStream, Encoding.UTF8))
                {
                    String responseString = reader.ReadToEnd();
                }

            }
            catch(Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
}

public static HttpWebRequest Create(string uri, string method)
        {
            var rq = (HttpWebRequest)WebRequest.Create(uri);
            rq.Method = method;
            rq.MaximumAutomaticRedirections = 4;
            rq.MaximumResponseHeadersLength = 4;
            rq.Headers.Add(new NameValueCollection() {
                {"Accept-Encoding", "gzip, deflate"},
                {"Accept-Language", "en-US,en;q=0.5"},
                {"Cache-Control", "no-cache"},
                {"Pragma", "no-cache"}
            });
            rq.Accept = "application/json";
            rq.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:22.0) Gecko/20100101 Firefox/22.0";
            rq.KeepAlive = true;
            rq.AllowAutoRedirect = true;
            return rq;
        }

Basic Authorization is being used in this case. There are many more authentication scheme can be use as below.

The most common authentication scheme is the “Basic” authentication scheme which is introduced in more details below. IANA maintains a list of authentication schemes, but there are other schemes offered by host services, such as Amazon AWS. Common authentication schemes include:

  • Basic (base64-encoded credentials)
  • Bearer (bearer tokens to access OAuth 2.0-protected resources)
  • Digest (only md5 hashing is supported in Firefox, see bug 472823 for SHA encryption support)
  • HOBA (HTTP Origin-Bound Authentication, digital-signature-based)
  • MutualĀ 

To Read More: HTTP authentication

Leave a Reply

Your email address will not be published. Required fields are marked *

×