使用C#.NET的SoundCloud与API连接和上传曲目 [英] Connecting to and uploading tracks with Soundcloud API using C# .NET

查看:200
本文介绍了使用C#.NET的SoundCloud与API连接和上传曲目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图上传使用C#.NET音轨到Soundcloud.com,但目前还没有对.NET任何资源的任何地方。可能有人张贴一个链接或如何使用.NET的音频文件上传到我的帐户Soundcloud.com一个例子?

I'm trying to upload an audio track to the Soundcloud.com using C#.NET, but there aren't any resources for .NET anywhere. Could someone post a link or an example of how to upload an audio file to my Soundcloud.com account using .NET?

感谢您,
阿尔曼

Thank you, Arman

推荐答案

要上传的SoundCloud使用的REST API,你需要照顾HTTP POST有关的问题(RFC 1867)的音频。一般情况下,ASP.NET不支持使用POST多个文件/值发送,所以我建议你使用Krystalware库:<一href=\"http://aspnetupload.com/Upload-File-POST-HttpWebRequest-WebClient-RFC-1867.aspx\">http://aspnetupload.com/Upload-File-POST-HttpWebRequest-WebClient-RFC-1867.aspx

To upload an audio using soundcloud's REST API you need to take care of HTTP POST related issues (RFC 1867). In general, ASP.NET does not support sending of multiple files/values using POST, so I suggest you to use Krystalware library: http://aspnetupload.com/Upload-File-POST-HttpWebRequest-WebClient-RFC-1867.aspx

之后,你需要适当的表单域发送到 https://api.soundcloud.com/tracks 网址:

After that you need to send proper form fields to the https://api.soundcloud.com/tracks url:


  • 验证令牌(组oauth_token)

  • 歌曲名称(曲目[标题])

  • 文件(轨道[asset_data])

样code:

using Krystalware.UploadHelper;
...

System.Net.ServicePointManager.Expect100Continue = false;
var request = WebRequest.Create("https://api.soundcloud.com/tracks") as HttpWebRequest;
//some default headers
request.Accept = "*/*";
request.Headers.Add("Accept-Charset", "ISO-8859-1,utf-8;q=0.7,*;q=0.3");
request.Headers.Add("Accept-Encoding", "gzip,deflate,sdch");
request.Headers.Add("Accept-Language", "en-US,en;q=0.8,ru;q=0.6");

//file array
var files = new UploadFile[] { 
    new UploadFile(Server.MapPath("Downloads//0.mp3"), "track[asset_data]", "application/octet-stream") 
};
//other form data
var form = new NameValueCollection();
form.Add("track[title]", "Some title");
form.Add("track[sharing]", "private");
form.Add("oauth_token", this.Token);
form.Add("format", "json");

form.Add("Filename", "0.mp3");
form.Add("Upload", "Submit Query");
try
{
    using (var response = HttpUploadHelper.Upload(request, files, form))
    {
        using (var reader = new StreamReader(response.GetResponseStream()))
        {
            lblInfo.Text = reader.ReadToEnd();
        }
    }
}
catch (Exception ex)
{
    lblInfo.Text = ex.ToString();
}

这个例子code可让您从服务器上传的音频文件(注意Server.MapPath方法形成的文件路径),并获得JSON格式(reader.ReadToEnd)的响应

The example code allows you to upload an audio file from the server (notice the Server.MapPath method to form path to the file) and to get a response in json format (reader.ReadToEnd)

这篇关于使用C#.NET的SoundCloud与API连接和上传曲目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆