How to consume rest api in C#

17,191

This all very much depend on the API's documentation, but to write data to the request body, get the request stream and then write the string to the stream. again, this depends on what the API you are authenticating with and without knowing which one is guesswork on my part.

        string sURL = "https://sampleurl.com/api1/token";
        WebRequest wrGETURL;
        wrGETURL = WebRequest.Create(sURL);

        wrGETURL.Method = "POST";
        wrGETURL.ContentType = @"application/json; charset=utf-8";
        using (var stream = new StreamWriter(wrGETURL.GetRequestStream()))
        {
            var bodyContent = new
            {
                username = "Actualusername",
                password = "Actualpassword"
            }; // This will need to be changed to an actual class after finding what the specification sheet requires.

            var json = JsonConvert.SerializeObject(bodyContent);

            stream.Write(json);
        }
        HttpWebResponse webresponse = wrGETURL.GetResponse() as HttpWebResponse;

        Encoding enc = System.Text.Encoding.GetEncoding("utf-8");
        // read response stream from response object
        StreamReader loResponseStream = new StreamReader(webresponse.GetResponseStream(), enc);
        // read string from stream data
        string strResult = loResponseStream.ReadToEnd();
        // close the stream object
        loResponseStream.Close();
        // close the response object
        webresponse.Close();

        Response.Write(strResult);
Share:
17,191
Jui Test
Author by

Jui Test

Updated on June 07, 2022

Comments

  • Jui Test
    Jui Test almost 2 years

    I want to consume rest api having post method in my project (web)/Windows service(C#).

    Url : https://sampleurl.com/api1/token
    

    I need to pass username and password for generating token. I have written code like this.

    string sURL = "https://sampleurl.com/api1/token/Actualusername/Actualpassword";
                WebRequest wrGETURL;
                wrGETURL = WebRequest.Create(sURL);
    
                wrGETURL.Method = "POST";
                wrGETURL.ContentType = @"application/json; charset=utf-8";
                wrGETURL.ContentLength = 0;
                HttpWebResponse webresponse = wrGETURL.GetResponse() as HttpWebResponse;
    
                Encoding enc = System.Text.Encoding.GetEncoding("utf-8");
                // read response stream from response object
                StreamReader loResponseStream = new StreamReader(webresponse.GetResponseStream(), enc);
                // read string from stream data
                string  strResult = loResponseStream.ReadToEnd();
                // close the stream object
                loResponseStream.Close();
                // close the response object
                webresponse.Close();
    
                Response.Write(strResult);
    

    I am getting error: No connection could be made because the target machine actively refused it

    Is it right way to consume rest api in C#?

  • Jui Test
    Jui Test almost 7 years
    I am using third party Rest api which made in PHP having post method.I don't know apart from this any other specification.
  • Jui Test
    Jui Test almost 7 years
    Thanks Robjam for your help.Let me checked with ur code and revert u back.
  • Sir Rufo
    Sir Rufo almost 7 years
    @JuiTest An API needs documentation. As a real world example this reads like "How to use my red car?"
  • Jui Test
    Jui Test almost 7 years
    Thanks Robjam for code.I used ur code.I am getting Error :'An existing connection was forcibly closed by the remote host '
  • robjam
    robjam almost 7 years
    I can't really help anymore unless I can see the documentation.