I tried to take the JSON string by using the API. I tried by HttpWebResponse
and WebRequest
. But Both method I got the exception The remote server returned an error: (401) Unauthorized.
This exception might be the username and password. But I using correct credential.I don`t know where I did the mistake.
My code is:
string URL = @"http://abc.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());
}
Where I did the mistake?
Thanks in advance.
You are doing something wrong here:
request.Headers["Authorization"]=
Basic+
Convert.ToBase64String(Encoding.Default.GetBytes("userName:passWord"));
The value you set above is not the one expected by the API.