Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove Authorization header from the redirect call #5

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 35 additions & 13 deletions AzureBillingApi/Client.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;

Expand All @@ -23,7 +24,7 @@ namespace CodeHollow.AzureBillingApi
public class Client
{
// correct usage of HttpClient as described here: https://aspnetmonsters.com/2016/08/2016-08-27-httpclientwrong/
private static readonly HttpClient _httpClient = new HttpClient();
private static readonly HttpClient _httpClient = new HttpClient(new HttpClientHandler{ AllowAutoRedirect = false });

#region Properties

Expand Down Expand Up @@ -241,24 +242,45 @@ protected static string GetData(string url, string token)
using (var request = new HttpRequestMessage(HttpMethod.Get, url))
{
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", token);
var response = DoRequest(request);

HttpResponseMessage response = _httpClient.SendAsync(request).Result;

if (!response.IsSuccessStatusCode)
if (response.StatusCode == HttpStatusCode.Found)
{
string errorMsg = "An error occurred! The service returned: " + response.ToString();

var x = response.Content.ReadAsStringAsync();
x.Wait();
errorMsg += "Content: " + x.Result;
throw new Exception(errorMsg);
var location = response.Headers.Location;
using (var redirect = new HttpRequestMessage(HttpMethod.Get, location))
{
response = DoRequest(redirect);
return GetContentString(response);
}
}

var readTask = response.Content.ReadAsStringAsync();
readTask.Wait();
return readTask.Result;
return GetContentString(response);
}
}

private static string GetContentString(HttpResponseMessage response)
{
var readTask = response.Content.ReadAsStringAsync();
readTask.Wait();
return readTask.Result;
}

private static HttpResponseMessage DoRequest(HttpRequestMessage request)
{
var response = _httpClient.SendAsync(request).Result;

if (!response.IsSuccessStatusCode)
{
var errorMsg = "An error occurred! The service returned: " + response.ToString();

var x = response.Content.ReadAsStringAsync();
x.Wait();
errorMsg += "Content: " + x.Result;
throw new Exception(errorMsg);
}

return response;
}

/// <summary>
/// Returns the costs for the given quantityToAdd for one billing cycle.
Expand Down