private string DownloadJson(string jsonAddress, SpotifyAddressContactType spotifyAddressContactType) { using (WebClientWithShortTimeout jsonWebClient = new WebClientWithShortTimeout()) { try { // Authorization uses POST instead of GET bool usePostMethodInsteadOfGet = false; string postParameters = string.Empty; // Modify HTTP headers based on what's being contacted switch (spotifyAddressContactType) { case SpotifyAddressContactType.Authorization: usePostMethodInsteadOfGet = true; postParameters = "grant_type=client_credentials"; jsonWebClient.Headers.Add("Content-Type", "application/x-www-form-urlencoded"); jsonWebClient.Headers.Add("Authorization", string.Format(CultureInfo.InvariantCulture, "Basic {0}", ApplicationKeys.Spotify)); break; case SpotifyAddressContactType.API: jsonWebClient.Headers.Add("Authorization", string.Format(CultureInfo.InvariantCulture, "Bearer {0}", this.authorizationToken)); break; default: break; } // Let's be respectful and identify ourself jsonWebClient.Headers.Add("User-Agent", "Snip/" + AssemblyInformation.AssemblyVersion); jsonWebClient.Encoding = Encoding.UTF8; string downloadedJson = string.Empty; if (usePostMethodInsteadOfGet) { downloadedJson = jsonWebClient.UploadString(jsonAddress, "POST", postParameters); } else { downloadedJson = jsonWebClient.DownloadString(jsonAddress); } if (!string.IsNullOrEmpty(downloadedJson)) { return(downloadedJson); } else { return(string.Empty); } } catch (WebException) { return(string.Empty); } } }
private string DownloadJson(string jsonAddress, SpotifyAddressContactType spotifyAddressContactType) { using (WebClientWithShortTimeout jsonWebClient = new WebClientWithShortTimeout()) { try { // Authorization uses POST instead of GET bool usePostMethodInsteadOfGet = false; string postParameters = string.Empty; // Modify HTTP headers based on what's being contacted switch (spotifyAddressContactType) { case SpotifyAddressContactType.Authorization: usePostMethodInsteadOfGet = true; postParameters = string.Format( CultureInfo.InvariantCulture, "grant_type=authorization_code&code={0}&redirect_uri={1}", this.authorizationCode, this.callbackAddress); jsonWebClient.Headers.Add("Content-Type", "application/x-www-form-urlencoded"); jsonWebClient.Headers.Add("Authorization", string.Format(CultureInfo.InvariantCulture, "Basic {0}", ApplicationKeys.Spotify)); break; case SpotifyAddressContactType.AuthorizationRefresh: usePostMethodInsteadOfGet = true; postParameters = string.Format( CultureInfo.InvariantCulture, "grant_type=refresh_token&refresh_token={0}", this.refreshToken); jsonWebClient.Headers.Add("Content-Type", "application/x-www-form-urlencoded"); jsonWebClient.Headers.Add("Authorization", string.Format(CultureInfo.InvariantCulture, "Basic {0}", ApplicationKeys.Spotify)); break; case SpotifyAddressContactType.API: jsonWebClient.Headers.Add("Authorization", string.Format(CultureInfo.InvariantCulture, "Bearer {0}", this.authorizationToken)); break; default: break; } // Let's be respectful and identify ourself jsonWebClient.Headers.Add("User-Agent", "Snip/" + AssemblyInformation.AssemblyVersion); jsonWebClient.Encoding = Encoding.UTF8; string downloadedJson = string.Empty; if (usePostMethodInsteadOfGet) { downloadedJson = jsonWebClient.UploadString(jsonAddress, "POST", postParameters); } else { downloadedJson = jsonWebClient.DownloadString(jsonAddress); } if (!string.IsNullOrEmpty(downloadedJson)) { return(downloadedJson); } else { return(string.Empty); } } catch (WebException webException) { WebResponse webResponse = webException.Response; WebHeaderCollection webHeaderCollection = webResponse.Headers; for (int i = 0; i < webHeaderCollection.Count; i++) { if (webHeaderCollection.GetKey(i).ToUpperInvariant() == "RETRY-AFTER") { // Set the timer to the retry seconds. Plus 1 for safety. this.updateSpotifyTrackInformation.Enabled = false; this.updateSpotifyTrackInformation.Interval = (Double.Parse(webHeaderCollection.Get(i) + 1, CultureInfo.InvariantCulture)) * 1000; this.updateSpotifyTrackInformation.Enabled = true; } } return(string.Empty); } } }
private string DownloadJson(string jsonAddress, SpotifyAddressContactType spotifyAddressContactType) { using (WebClientWithShortTimeout jsonWebClient = new WebClientWithShortTimeout()) { try { // Auth uses POST instead of GET bool usePostMethodInsteadOfGet = false; string postParameters = string.Empty; // Modify HTTP headers based on what's being contacted switch (spotifyAddressContactType) { case SpotifyAddressContactType.Authorization: Global.debugConsole?.WriteLine("DownloadJson type Auth."); usePostMethodInsteadOfGet = true; postParameters = string.Format(CultureInfo.InvariantCulture, "grant_type=authorization_code&code={0}&redirect_uri={1}", this.authorizationCode, this.localCallbackURL); jsonWebClient.Headers.Add("Content-Type", "application/x-www-form-urlencoded"); jsonWebClient.Headers.Add("Authorization", string.Format(CultureInfo.InvariantCulture, "Basic {0}", this.spotify64BitKey)); break; case SpotifyAddressContactType.AuthorizationRefresh: Global.debugConsole?.WriteLine("DownloadJson type AuthRefresh."); usePostMethodInsteadOfGet = true; postParameters = string.Format(CultureInfo.InvariantCulture, "grant_type=refresh_token&refresh_token={0}", this.refreshToken); jsonWebClient.Headers.Add("Content-Type", "application/x-www-form-urlencoded"); jsonWebClient.Headers.Add("Authorization", string.Format(CultureInfo.InvariantCulture, "Basic {0}", this.spotify64BitKey)); break; case SpotifyAddressContactType.API: jsonWebClient.Headers.Add("Authorization", string.Format(CultureInfo.InvariantCulture, "Bearer {0}", this.authorizationToken)); break; default: break; } jsonWebClient.Headers.Add("User-Agent", "SnipForMammal/v1"); jsonWebClient.Encoding = Encoding.UTF8; string downloadedJson = string.Empty; if (usePostMethodInsteadOfGet) { downloadedJson = jsonWebClient.UploadString(jsonAddress, "POST", postParameters); } else { downloadedJson = jsonWebClient.DownloadString(jsonAddress); } return(downloadedJson); } catch (WebException webException) { Global.debugConsole?.WriteLine("WebException thrown in DownloadJson()"); Global.debugConsole?.WriteLine(" Exception Message: " + webException.Message); // Catch "(503) Server Unavailable" WebResponse webResponse = webException.Response; WebHeaderCollection webHeaderCollection = webResponse.Headers; for (int i = 0; i < webHeaderCollection.Count; i++) { if (webHeaderCollection.GetKey(i).ToUpperInvariant() == "RETRY-AFTER") { // Set the timer to the retry seconds. Plus 1 for safety. this.updateSpotifyTrackInfo.Enabled = false; this.updateSpotifyTrackInfo.Interval = (Double.Parse(webHeaderCollection.Get(i) + 2, CultureInfo.InvariantCulture)) * 1000; this.updateSpotifyTrackInfo.Enabled = true; } } return(string.Empty); } catch (Exception e) { Global.debugConsole?.WriteLine("Generic exception thrown in DownloadJson()"); Global.debugConsole?.WriteLine(" Exception Message: " + e.Message); return(string.Empty); } } }