3

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.

  • 1
    I thing, that when you use "request.Headers" there is no need to set "request.PreAuthenticate = true;". At least that how it works in my code (not for gitlab but another server). (But I use "request.Headers.Add"). And you code should contain "request.Method = "GET";" – Julo Jan 12 '17 at 07:42
  • Start by actually reading the error message : Unauthorized. That means you used the wrong credentials. Why don't you use the `Credentials` property as shown here [How to use HttpWebRequest.Credentials Property for Basic Authentication?](http://stackoverflow.com/questions/19764113/how-to-use-httpwebrequest-credentials-property-for-basic-authentication) – Panagiotis Kanavos Jan 12 '17 at 07:52
  • Also, why are you using HttpWebRequest instead of eg HttpClient? All those lines could be reduced to 3-4 – Panagiotis Kanavos Jan 12 '17 at 07:53
  • @Julo I Added the request.method in my code and run it. But now also i facing same error. –  Jan 12 '17 at 08:58
  • @Arunald Unfortunately I only started this week with web request testing (for a specific project). In my response I only wrote the differences found betwenn your code an the code that works for me (on different server). The page has a API for authorization (/auth) with Basic method, where I POST (command) my user data (name/password) through JSON data. As response i get a token, This token is then used as authorization token for other API functions. – Julo Jan 12 '17 at 09:50
  • @Julo I worked by using the POST command in other website and got expected output. But This is my first attempt using GET command the data. –  Jan 12 '17 at 10:22

1 Answers1

8

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");
Joey Erdogan
  • 184
  • 1
  • 13
  • I tried both changes in my code. but I face the same exception for both changes (401 unauthorized) –  Jan 12 '17 at 08:55
  • 1
    I suggest you to get in contact with syncfusion and ask them what kind of authentication is required to access the page. Seems they do not use basic authentication methods. – Joey Erdogan Jan 12 '17 at 09:12