mirror of
https://github.com/Sarsoo/Spotify.NET.git
synced 2024-12-24 23:16:28 +00:00
Fixed Unhandled Exceptions Thrown by Web Client
Unhandled exception with the message "WebClient does not support concurrent I/O operations" being thrown by query Created asynchronous query function, and additionally created separate instances of query for each method.
This commit is contained in:
parent
faa7542259
commit
3cf603b1ad
@ -18,21 +18,28 @@ namespace SpotifyAPI.SpotifyLocalAPI
|
|||||||
|
|
||||||
public String host = "127.0.0.1";
|
public String host = "127.0.0.1";
|
||||||
|
|
||||||
ExtendedWebClient wc;
|
private class ExtendedWebClientInstance : ExtendedWebClient
|
||||||
|
{
|
||||||
|
internal ExtendedWebClientInstance()
|
||||||
|
{
|
||||||
|
Timeout = 2000;
|
||||||
|
Proxy = null;
|
||||||
|
Headers.Add("Origin", "https://embed.spotify.com");
|
||||||
|
Headers.Add("Referer", "https://embed.spotify.com/?uri=spotify:track:5Zp4SWOpbuOdnsxLqwgutt");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
internal static RemoteHandler GetInstance()
|
internal static RemoteHandler GetInstance()
|
||||||
{
|
{
|
||||||
|
// Is this supposed to return the current instance?
|
||||||
return instance;
|
return instance;
|
||||||
}
|
}
|
||||||
|
|
||||||
internal RemoteHandler()
|
internal RemoteHandler()
|
||||||
{
|
{
|
||||||
|
|
||||||
wc = new ExtendedWebClient();
|
|
||||||
wc.Timeout = 2000;
|
|
||||||
wc.Proxy = null;
|
|
||||||
wc.Headers.Add("Origin", "https://embed.spotify.com");
|
|
||||||
wc.Headers.Add("Referer", "https://embed.spotify.com/?uri=spotify:track:5Zp4SWOpbuOdnsxLqwgutt");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
internal Boolean Init()
|
internal Boolean Init()
|
||||||
{
|
{
|
||||||
oauthKey = GetOAuthKey();
|
oauthKey = GetOAuthKey();
|
||||||
@ -41,22 +48,27 @@ namespace SpotifyAPI.SpotifyLocalAPI
|
|||||||
return false;
|
return false;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
internal void SendPauseRequest()
|
|
||||||
|
internal async void SendPauseRequest()
|
||||||
{
|
{
|
||||||
query("remote/pause.json?pause=true", true, true, -1);
|
await QueryAsync("remote/pause.json?pause=true", true, true, -1);
|
||||||
}
|
}
|
||||||
internal void SendPlayRequest()
|
|
||||||
|
internal async void SendPlayRequest()
|
||||||
{
|
{
|
||||||
query("remote/pause.json?pause=false", true, true, -1);
|
await QueryAsync("remote/pause.json?pause=false", true, true, -1);
|
||||||
}
|
}
|
||||||
internal void SendPlayRequest(String url)
|
|
||||||
|
internal async void SendPlayRequest(String url)
|
||||||
{
|
{
|
||||||
query("remote/play.json?uri=" + url, true, true, -1);
|
await QueryAsync("remote/play.json?uri=" + url, true, true, -1);
|
||||||
}
|
}
|
||||||
internal void SendQueueRequest(String url)
|
|
||||||
|
internal async void SendQueueRequest(String url)
|
||||||
{
|
{
|
||||||
query("remote/play.json?uri=" + url + "?action=queue", true, true, -1);
|
await QueryAsync("remote/play.json?uri=" + url + "?action=queue", true, true, -1);
|
||||||
}
|
}
|
||||||
|
|
||||||
internal StatusResponse Update()
|
internal StatusResponse Update()
|
||||||
{
|
{
|
||||||
String response = query("remote/status.json", true, true, -1);
|
String response = query("remote/status.json", true, true, -1);
|
||||||
@ -70,6 +82,7 @@ namespace SpotifyAPI.SpotifyLocalAPI
|
|||||||
List<StatusResponse> raw = (List<StatusResponse>)JsonConvert.DeserializeObject(response, typeof(List<StatusResponse>));
|
List<StatusResponse> raw = (List<StatusResponse>)JsonConvert.DeserializeObject(response, typeof(List<StatusResponse>));
|
||||||
return raw[0];
|
return raw[0];
|
||||||
}
|
}
|
||||||
|
|
||||||
internal String GetOAuthKey()
|
internal String GetOAuthKey()
|
||||||
{
|
{
|
||||||
String raw = "";
|
String raw = "";
|
||||||
@ -82,7 +95,7 @@ namespace SpotifyAPI.SpotifyLocalAPI
|
|||||||
return (String)lol["t"];
|
return (String)lol["t"];
|
||||||
}
|
}
|
||||||
|
|
||||||
internal String GetCFID()
|
internal string GetCFID()
|
||||||
{
|
{
|
||||||
string a = query("simplecsrf/token.json", false, false, -1);
|
string a = query("simplecsrf/token.json", false, false, -1);
|
||||||
a = a.Replace(@"\", "");
|
a = a.Replace(@"\", "");
|
||||||
@ -95,7 +108,8 @@ namespace SpotifyAPI.SpotifyLocalAPI
|
|||||||
return "";
|
return "";
|
||||||
return d[0].token;
|
return d[0].token;
|
||||||
}
|
}
|
||||||
internal String query(string request, bool oauth, bool cfid, int wait)
|
|
||||||
|
internal string query(string request, bool oauth, bool cfid, int wait)
|
||||||
{
|
{
|
||||||
string parameters = "?&ref=&cors=&_=" + GetTimestamp();
|
string parameters = "?&ref=&cors=&_=" + GetTimestamp();
|
||||||
if (request.Contains("?"))
|
if (request.Contains("?"))
|
||||||
@ -123,15 +137,59 @@ namespace SpotifyAPI.SpotifyLocalAPI
|
|||||||
try
|
try
|
||||||
{
|
{
|
||||||
//Need to find a better solution
|
//Need to find a better solution
|
||||||
|
var wc = new ExtendedWebClientInstance();
|
||||||
if (SpotifyLocalAPIClass.IsSpotifyRunning())
|
if (SpotifyLocalAPIClass.IsSpotifyRunning())
|
||||||
response = "[ " + wc.DownloadString(a) + " ]";
|
response = "[ " + wc.DownloadString(a) + " ]";
|
||||||
}
|
}
|
||||||
catch (Exception z)
|
catch (Exception z)
|
||||||
{
|
{
|
||||||
|
Console.WriteLine(z.Message);
|
||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
return response;
|
return response;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
internal async Task<string> QueryAsync(string request, bool oauth, bool cfid, int wait)
|
||||||
|
{
|
||||||
|
string parameters = "?&ref=&cors=&_=" + GetTimestamp();
|
||||||
|
if (request.Contains("?"))
|
||||||
|
{
|
||||||
|
parameters = parameters.Substring(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (oauth)
|
||||||
|
{
|
||||||
|
parameters += "&oauth=" + oauthKey;
|
||||||
|
}
|
||||||
|
if (cfid)
|
||||||
|
{
|
||||||
|
parameters += "&csrf=" + cfidKey;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (wait != -1)
|
||||||
|
{
|
||||||
|
parameters += "&returnafter=" + wait;
|
||||||
|
parameters += "&returnon=login%2Clogout%2Cplay%2Cpause%2Cerror%2Cap";
|
||||||
|
}
|
||||||
|
|
||||||
|
string a = "http://" + host + ":4380/" + request + parameters;
|
||||||
|
string response = "";
|
||||||
|
try
|
||||||
|
{
|
||||||
|
//Need to find a better solution
|
||||||
|
var wc = new ExtendedWebClientInstance();
|
||||||
|
if (SpotifyLocalAPIClass.IsSpotifyRunning())
|
||||||
|
response = "[ " + await wc.DownloadStringTaskAsync(new Uri(a)) + " ]";
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
Console.WriteLine(ex.Message);
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
|
return response;
|
||||||
|
}
|
||||||
|
|
||||||
internal int GetTimestamp()
|
internal int GetTimestamp()
|
||||||
{
|
{
|
||||||
return Convert.ToInt32((DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0)).TotalSeconds);
|
return Convert.ToInt32((DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0)).TotalSeconds);
|
||||||
|
Loading…
Reference in New Issue
Block a user