I'm writing a small C# program to do some API calls against Spotifys API.
However I'm running into a problem because I need to call different End Points. I had thought I could simply re-set the HTTP Client instance Base Address for each call but this doesn't appear to be the case, or I'm trying to do it incorrectly.
Some googling leads me to believe I may actually be using HTTP Client incorrectly (I'm avoiding ever using await) but before I go down the path of tearing all my work apart I'd like to confirm I'm not just missing something obvious. I've marked the point the code fails with a comment.
//initialize client
HttpClient APIclient = new HttpClient();
APIclient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
APIclient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", DeserializedToken.access_token.ToString());
//API Call of User Info
//setup clients base url
UriBuilder myUriBase = new UriBuilder(baseURIMethod, baseURI);
myUriBase.Path = "me";
APIclient.BaseAddress = new Uri(myUriBase.ToString());
HttpResponseMessage apiresponse = APIclient.GetAsync(APIclient.BaseAddress.ToString()).Result;
TokenHelper tokenrefresh = new TokenHelper(authCode, redirectURI);
tokenrefresh.SetupRefreshKeys(DeserializedToken.refresh_token.ToString());
tokenrefresh.SetMessageContent();
//Send token request
//client is instantiated above, not shown for code brevity
HttpResponseMessage RefreshResponse = client.SendAsync(tokenrefresh.TokenRequest).Result;
DeserializedToken = JsonConvert.DeserializeObject<TokenResponse>(RefreshResponse.Content.ReadAsStringAsync().Result);
APIclient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", DeserializedToken.access_token.ToString());
myUriBase.Path = "me/playlists";
Uri testing = new Uri(myUriBase.ToString());
// CODE FAILS HERE with This instance has already started one or more requests. Properties can only be modified before sending the first request.
APIclient.BaseAddress = new Uri(myUriBase.ToString());
apiresponse = APIclient.GetAsync(APIclient.BaseAddress.ToString()).Result;
Console.WriteLine(apiresponse.Content.ReadAsStringAsync().Result);
Console.ReadKey();