public static Response SendGetRequest(string url, object data, bool allowAutoRedirect) { string paramData = string.Empty; HttpCommunications.GetProperties(data).ToList().ForEach(x => { paramData += string.Format("{0}={1}&", x.Key, x.Value); }); paramData = paramData.TrimEnd('&'); url = ProcessUrl(url); HttpWebRequest request = paramData.Length > 0 ? (HttpWebRequest)WebRequest.Create(string.Format("{0}?{1}", url, paramData)) : (HttpWebRequest)WebRequest.Create(url); request.AllowAutoRedirect = allowAutoRedirect; return(new Response(GetResponse(request))); }
public static Response SendPostRequest(string url, object data, bool allowAutoRedirect) { try { string formData = string.Empty; HttpCommunications.GetProperties(data).ToList().ForEach(x => { string key = x.Key; if (x.Key == "newone") { // this is a workaround as new is a command in C#... key = "new"; } formData += string.Format("{0}={1}&", key, x.Value); }); formData = formData.TrimEnd('&'); url = ProcessUrl(url); HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url); request.Method = "POST"; request.AllowAutoRedirect = allowAutoRedirect; request.Accept = "*/*"; request.UserAgent = "last.fm snarls (http://www.tlhan-ghun.de/)"; request.ContentType = "application/x-www-form-urlencoded"; byte[] encodedData = new UTF8Encoding().GetBytes(formData); request.ContentLength = encodedData.Length; using (Stream newStream = request.GetRequestStream()) { newStream.Write(encodedData, 0, encodedData.Length); } return(new Response(GetResponse(request))); } catch (System.Exception e) { return(null); } }
static void monitorUser() { int lastConnectionErrorId = 0; //MD5Hash key = new MD5Hash("1ca43b465fcb2306f51f4df7c010067c", true, Encoding.ASCII); //MD5Hash secret = new MD5Hash("8c225ea33ff798e680368c2cd45a5f1e", true, Encoding.ASCII); //AuthData myAuth = new AuthData(key, secret); //Settings20.AuthData = myAuth; // LastFmClient client = LastFmClient.Create(myAuth); // client.LastFmUser.Username = "******"; // client.LastFmUser.EncryptAndSetPassword("xxxx"); Track lastPlaying = new Track(); Track lastRecent = new Track();; bool directlyAfterStart = true; while (true) { try { Response lastFmData = HttpCommunications.SendGetRequest("http://ws.audioscrobbler.com/2.0/", new { method = "user.getrecenttracks", user = userNameString, api_key = "1ca43b465fcb2306f51f4df7c010067c", limit = 2 }, false); if (!lastFmData.Success) { lastConnectionErrorId = SnarlConnector.ShowMessageEx("last.fm error", "last.fm API error", lastFmData.ErrorText, 20, iconPath, hwnd, Snarl.WindowsMessage.WM_USER + 13, ""); } else { Track nowPlaying = lastFmData.NowPlaying; Track lastTrack = lastFmData.LastPlayed; if (nowPlaying != null && lastPlaying.Name != nowPlaying.Name) { string artworkPath = nowPlaying.getBestAlbumArt(); if (artworkPath == "") { artworkPath = iconPath; } SnarlConnector.ShowMessageEx("Now being played track", nowPlaying.Artist, nowPlaying.Name + "\n\n" + nowPlaying.Album, 10, artworkPath, hwnd, Snarl.WindowsMessage.WM_USER + 11, ""); snarlComWindow.currentUrl = nowPlaying.Link; lastPlaying = nowPlaying; } if (lastTrack != null && lastRecent.Name != lastTrack.Name) { if (!directlyAfterStart) { string artworkPath = lastTrack.getBestAlbumArt(); if (artworkPath == "") { artworkPath = iconPath; } SnarlConnector.ShowMessageEx("Recently played track", lastTrack.Artist, lastTrack.Name + "\n\n" + lastTrack.Album, 10, artworkPath, hwnd, Snarl.WindowsMessage.WM_USER + 12, ""); snarlComWindow.recentUrl = lastTrack.Link; } lastRecent = lastTrack; } directlyAfterStart = false; } Thread.Sleep(1000); } catch (Exception exp) { if (lastConnectionErrorId == 0 && exp.Message != "Thread was being aborted.") { lastConnectionErrorId = SnarlConnector.ShowMessageEx("Connection error", "Connection to last.fm failed", "Connection to last.fm can't be established. Maybe the site is down or your internet connection is not available.\n\n" + exp.Message, 20, iconPath, hwnd, Snarl.WindowsMessage.WM_USER + 13, ""); if (DEBUG) { SnarlConnector.ShowMessageEx("Debug message", "Error message", exp.Message, 0, iconPath, hwnd, Snarl.WindowsMessage.WM_USER + 77, ""); SnarlConnector.ShowMessageEx("Debug message", "Error source", exp.Source, 0, iconPath, hwnd, Snarl.WindowsMessage.WM_USER + 77, ""); SnarlConnector.ShowMessageEx("Debug message", "Stack trace", exp.StackTrace.Substring(0, 500), 0, iconPath, hwnd, Snarl.WindowsMessage.WM_USER + 77, ""); } } } } }