使用c#将音频文件上传到soundcloud

时间:2014-03-07 14:53:23

标签: c# asp.net asp.net-mvc c#-4.0 soundcloud

WebClient client = new WebClient();
string postData = "client_id=" + "b408123adf1e3a950876d84475587ca2"
                  + "&client_secret=" + "d74a342169f5f5b369622d582f77b09e"
                  + "&grant_type=password&username=" + "biksad" //your username
                  + "&password=" + "369789";//your password :)
string soundCloudTokenRes = "https://api.soundcloud.com/oauth2/token";
string tokenInfo = client.UploadString(soundCloudTokenRes, postData);
System.Net.ServicePointManager.Expect100Continue = false;
var request = WebRequest.Create("https://soundcloud.com/biksad/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(filePath, "@" + fileName, "application/octet-stream") };

//other form data
var form = new NameValueCollection();
form.Add("track[title]", "biksad");
form.Add("track[sharing]", "public");
form.Add("oauth_token", tokenInfo);
form.Add("format", "json");
form.Add("Filename", fileName);
form.Add("Upload", "Submit Query");
string lblInfo;

try
{
    using (var response = HttpUploadHelper.Upload(request, files, form))
    {
        using (var reader = new StreamReader(response.GetResponseStream()))
        {
            lblInfo = reader.ReadToEnd();
        }
    }
}
catch (Exception ex)
{
    lblInfo = ex.ToString();
}

我想将音频文件从我的服务器上传到我的soundcloud帐户。我收到了这个错误:

  

在写入所有字节之前无法关闭流。

如何正确检测“表单”值(跟踪[标题],跟踪[共享]等)?

1 个答案:

答案 0 :(得分:0)

此链接将显示该POST上的所有字段的含义 http://developers.soundcloud.com/docs/api/reference#tracks
您还可以使用他们提供的工具来查看URL应包含的内容: http://developers.soundcloud.com/console