1

I'm trying to Upload Track to sound cloud. So far i'm successfully able to get auth token but when i tried to 'PostAsync' it throws exception

A Task was canceled. I have also set the timeout value but still the same error. Issue seems to be something wrong with what i'm doing. Below is the code which i'm using and i have taken it from stack overflow but with no luck.

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

Is there any thing i'm doing ? Can any one point out the issue in particular code.

Regards,

Muhammad Umar
  • 3,761
  • 1
  • 24
  • 36
  • See this answer: http://stackoverflow.com/a/29214208/881798 to find out if it's really a timeout. – vendettamit Dec 28 '15 at 21:49
  • The only reason I see is Timeout which tries to set CancellationToken unless user explicitly asked for it. see source [here](http://www.symbolsource.org/Public/Metadata/NuGet/Project/HttpClient/0.5.0/Release/.NETFramework,Version=v4.0/Microsoft.Net.Http/Microsoft.Net.Http/System/Net/Http/HttpClient.cs) may be you should try increasing the timeout period a little higher. – vendettamit Dec 28 '15 at 22:14
  • I have tried it already and yes it is timeout. I have increased the timeout value to 10min but still it won't work. I'm afraid there is something which i'm missing. – Muhammad Umar Dec 29 '15 at 06:47

0 Answers0