通过 API 下载 soundcloud 数据
Download souncloud data through API
我尝试通过他们的 API 从 soundcloud 检索 mp3 数据,我已经有了 download_url 属性,我想将它直接保存到 KnownFolders.MusicLibrary。
问题是当我尝试使用浏览器下载它时 link 要求 save/play。
我是否可以绕过这个并直接下载 link 而不必提示 select 保存或播放。
尝试使用 HttpClient 或 WebClient。
据我所知,WebClient 有一个 OpenReadAsync 方法。
使用此方法并将 mp3 作为参数传递 url。
你得到的是你的 mp3 流,然后你可以使用 StreamWriter 将它作为文件保存到本地文件夹中。
我自己没有试过,但是 "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;
}
}
谢谢@Ogglas,我使用了您的一些代码并成功检索了 cdn url :
var request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "GET";
var response = await request.GetResponseAsync();
var downloadurl = response.ResponseUri.AbsoluteUri;
我尝试通过他们的 API 从 soundcloud 检索 mp3 数据,我已经有了 download_url 属性,我想将它直接保存到 KnownFolders.MusicLibrary。 问题是当我尝试使用浏览器下载它时 link 要求 save/play。 我是否可以绕过这个并直接下载 link 而不必提示 select 保存或播放。
尝试使用 HttpClient 或 WebClient。 据我所知,WebClient 有一个 OpenReadAsync 方法。
使用此方法并将 mp3 作为参数传递 url。
你得到的是你的 mp3 流,然后你可以使用 StreamWriter 将它作为文件保存到本地文件夹中。
我自己没有试过,但是 "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;
}
}
谢谢@Ogglas,我使用了您的一些代码并成功检索了 cdn url :
var request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "GET";
var response = await request.GetResponseAsync();
var downloadurl = response.ResponseUri.AbsoluteUri;