Hello,
I’m a real newbie to requesting API data from here and using HTTP requests in General. I have an account with a application and key so far.
However, does anyone have a complete sample application written in C# to make a GET request and return the results.
Currently, I have some code written, but I’m getting a Authorisation error (407). Obviously, I need to set up and get my credentials working send the get request and return the results. I’m using the HttpWebRequest object. Can anyone help with this or have an sample code or tutorials on how to do this?
Actually, the error message is as follows.
“The remote server returned an error: (407) Proxy Authentication Required.”
Could this be to do with our proxy server? If so, can someone explain what is going on here and how I can overcome this?
Here is my code:
static void GetCompanyFromNumber()
{
string credentials = String.Format("{0}:{1}", "API_KEY", "XLO5bXXXXXX3Js86dDqkXuoXXXXXXP9CcWLnB33tN");
// Not my real API KEY....
byte[] bytes = Encoding.ASCII.GetBytes(credentials);
string base64 = Convert.ToBase64String(bytes);
string authorization = String.Concat("Basic ", base64);
try
{
// create my web request
HttpWebRequest webReq = (HttpWebRequest)WebRequest.Create("https://api.companieshouse.gov.uk/company/08261946");
// use default credentials
webReq.UseDefaultCredentials = true; webReq.Headers.Add("Authorization", authorization);
// sets the method as a get
webReq.Method = "GET";
// performs the request and gets the response
HttpWebResponse webResp = (HttpWebResponse)webReq.GetResponse();
// prints out the status code
Console.WriteLine(webResp.StatusCode);
// prints out the server
Console.WriteLine(webResp.Server);
// create a stream to the response
Stream answer = webResp.GetResponseStream();
// read the stream
StreamReader _answer = new StreamReader(answer);
// display the stream
Console.WriteLine(_answer.ReadToEnd());
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
Thanks in advance,
R.