Search code examples
c#windows-phone-8.1windows-phone

Download souncloud data through API


I try to retrieve mp3 data from soundcloud through their API, I already have the download_url property and I want to save it directly to KnownFolders.MusicLibrary. The problem is when I try to download it with browser the link ask for save/play. Is it possible for me to bypass this and get the direct download link without have to be prompted to select save or play.


Solution

  • I have not tried this myself but "download_url" is as you say a url to the original file. Try using this code and see what you get, then you can modify the request until you get the datastream. You could also try a proxy like Burp or Fiddler to examine what gets passed to Soundcloud and then create a similar request.

    public static string GetSoundCloudData()
    {
        var request = (HttpWebRequest)WebRequest.Create("http://api.soundcloud.com/tracks/3/download");
        request.Method = "GET";
        request.UserAgent =
            "Mozilla/5.0 (Windows NT 6.3; WOW64; rv:38.0) Gecko/20100101 Firefox/38.0";
        // Get the response.
        WebResponse response = request.GetResponse();
        // Display the status.
        if (((HttpWebResponse)response).StatusCode == HttpStatusCode.OK)
        {
            // Get the stream containing content returned by the server.
            var dataStream = response.GetResponseStream();
            // Open the stream using a StreamReader for easy access.
            StreamReader reader = new StreamReader(dataStream, Encoding.UTF8);
            // Read the content.
            string responseFromServer = reader.ReadToEnd();
            // Display the content.
            Debug.WriteLine(responseFromServer);
    
            // Clean up the streams.
            reader.Close();
            dataStream.Close();
            response.Close();
            return responseFromServer;
    
        }
        else
        {
            response.Close();
            return null;
        }
    }