通过API下载souncloud数据

本文关键字:数据 souncloud 下载 API 通过 | 更新日期: 2023-09-27 18:13:14

我尝试通过他们的API从soundcloud检索mp3数据,我已经有download_url属性,我想直接保存到KnownFolders.MusicLibrary。问题是,当我试图用浏览器下载它的链接要求保存/播放。我是否有可能绕过这个并获得直接下载链接,而不必提示选择保存或播放

通过API下载souncloud数据

我自己没有试过,但"download_url"是你说的原始文件的url。尝试使用这段代码,看看会得到什么,然后可以修改请求,直到得到数据流。你也可以尝试像Burp或Fiddler这样的代理来检查传递给Soundcloud的内容,然后创建一个类似的请求。

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;
    }
}

尝试使用HttpClient或WebClient。据我所知,WebClient有一个OpenReadAsync方法。

使用此方法并将mp3 url作为参数传递。

你将得到的是mp3的流,然后你可以使用StreamWriter将它保存到本地文件夹作为文件。

谢谢@Ogglas,我使用了您的一些代码并成功检索了cdn url:

var request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "GET";
var response = await request.GetResponseAsync();
var downloadurl = response.ResponseUri.AbsoluteUri;