C# how to get the JSON string by using HttpWebRequest

user6874415 picture user6874415 · Jan 12, 2017 · Viewed 41.4k times · Source

Anybody help me to get the JSON from the gitlab site. I have written my code but when I compile the code I receiving the exception The remote server returned an error: (401) Unauthorized. in my console application (C#).

In my browser, I run the same URL when the I signed in the browser, I can able to get the JSON string. If I run the same URL after signed out from the browser I get {"message":"401 Unauthorized"} message in my browser.

Due to this same exception, I think that I did not pass the username and credential into my HttpWebRequest.

I getting the exception in the line HttpWebResponse response = request.GetResponse() as HttpWebResponse;

My code:

            string URL = "http://gitlab.company.com/api/v3/users?per_page=100&page=1";  
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URL);
            request.ContentType = "application/json; charset=utf-8";
            request.Headers["Authorization"] = "Basic " + Convert.ToBase64String(Encoding.Default.GetBytes("userName:passWord"));
            request.PreAuthenticate = true;
            HttpWebResponse response = request.GetResponse() as HttpWebResponse;
            using (Stream responseStream = response.GetResponseStream())
            {
                StreamReader reader = new StreamReader(responseStream, Encoding.UTF8);
                Console.WriteLine(reader.ReadToEnd());
            }

My question:

  1. What and where did I made a mistake?
  2. I know I have to use the GET verb to get the data in API but I don`t know where I have to use the GET verb?

Thanks in advance.

Answer

Joey Erdogan picture Joey Erdogan · Jan 12, 2017

Use encoding in your request, credentials need to be encoded in ISO-8859-1

        string URL = "http://gitlab.company.com/api/v3/users?per_page=100&page=1";
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URL);
        request.ContentType = "application/json; charset=utf-8";
        request.Headers["Authorization"] = "Basic " + Convert.ToBase64String(Encoding.GetEncoding("ISO-8859-1").GetBytes("username:password"));
        request.PreAuthenticate = true;
        HttpWebResponse response = request.GetResponse() as HttpWebResponse;
        using (Stream responseStream = response.GetResponseStream())
        {
            StreamReader reader = new StreamReader(responseStream, Encoding.UTF8);
            Console.WriteLine(reader.ReadToEnd());
        }

If that does not work, remove the Headers line and replace it with the following:

request.Credentials = new NetworkCredential("username", "password");