将音频上传到SoundCloud

时间:2015-12-28 20:17:45

标签: c# soundcloud

我正在尝试将Track上传到声音云。到目前为止,我已成功获得身份验证令牌,但当我尝试'PostAsync'时,它会抛出异常

任务已取消。我也设置了超时值但仍然是相同的错误。问题似乎与我正在做的事情有关。下面是我正在使用的代码,我从堆栈溢出中获取它但没有运气。

private async Task<bool> StartUploadAsync(PubVidoInfo pubStuff)
    {
        HttpClient httpClient = new HttpClient();
        httpClient.DefaultRequestHeaders.ConnectionClose = true;
        httpClient.DefaultRequestHeaders.UserAgent.Add(new ProductInfoHeaderValue("Mixcraft", "1.0"));
        httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
        httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", AccessToken);
        ByteArrayContent titleContent = new ByteArrayContent(Encoding.UTF8.GetBytes(pubStuff.title));
        ByteArrayContent descriptionContent = new ByteArrayContent(Encoding.UTF8.GetBytes(pubStuff.description));

        ByteArrayContent sharingContent = null;
        if (pubStuff.publishedFileState == PubFileState.PF_Public)
        {
            sharingContent = new ByteArrayContent(Encoding.UTF8.GetBytes("public"));
        }
        else
        {
            sharingContent = new ByteArrayContent(Encoding.UTF8.GetBytes("private"));
        }

        ByteArrayContent byteArrayContent = new ByteArrayContent(File.ReadAllBytes(pubStuff.pubFilePath));
        byteArrayContent.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");

        MultipartFormDataContent content = new MultipartFormDataContent();
        content.Add(titleContent, "track[title]");
        content.Add(descriptionContent, "track[description]");
        content.Add(sharingContent, "track[sharing]");
        content.Add(byteArrayContent, "track[asset_data]", Path.GetFileName(pubStuff.pubFilePath));
        try
        {
            HttpResponseMessage message = await httpClient.PostAsync(new Uri("https://api.soundcloud.com/tracks"), content);

            if (message.IsSuccessStatusCode)
            {
                PubUploadStatus = PubUploadStatus.US_Finished;
            }
            else
            {
                PubUploadStatus = PubUploadStatus.US_Failed;
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
            PubUploadStatus = PubUploadStatus.US_Failed;
        }


        return true;
    }

我有什么事吗?任何人都可以在特定代码中指出问题。

此致

0 个答案:

没有答案