Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Twilio retrieve composition media

I'm trying to download a composition media file into my hard drive using the following code:

try
{
    var uri = "https://video.twilio.com/v1/Compositions/" + sid + "/Media?Ttl=6000";

    var request = (HttpWebRequest)WebRequest.Create(uri);
    request.Headers.Add("Authorization", "Basic " + Convert.ToBase64String(Encoding.ASCII.GetBytes(_apiKeySid + ":" + _apiKeySecret)));
    request.AllowAutoRedirect = false;
    var responseBody = new StreamReader(request.GetResponse().GetResponseStream()).ReadToEnd();
    var mediaLocation = JsonConvert.DeserializeObject<Dictionary<string, string>>(responseBody)["redirect_to"];

    new WebClient().DownloadFile(mediaLocation, "D:\\test.mp4");
}
catch (Exception ex)
{
    var temp = ex.Message;
}

But every time I get an exception with this message: "The remote server returned an error: (302) FOUND."

Note that this method is called after Twilio calls my StatusCallback method which I've set when creating a new composition using CompositionResource.CreateAsync method.

like image 324
Mo Sadeghipour Avatar asked Aug 03 '18 16:08

Mo Sadeghipour


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

Is C language easy?

Compared to other languages—like Java, PHP, or C#—C is a relatively simple language to learn for anyone just starting to learn computer programming because of its limited number of keywords.

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

Why is C named so?

Because a and b and c , so it's name is C. C came out of Ken Thompson's Unix project at AT&T. He originally wrote Unix in assembly language. He wrote a language in assembly called B that ran on Unix, and was a subset of an existing language called BCPL.


2 Answers

So, the problem was that the request was being redirected to a new location, so all I had to do was to allow redirects for the request and then download the file by copying the stream object to a file, like this:

        var uri = "https://video.twilio.com/v1/Compositions/" + sid + "/Media?Ttl=6000";

        var request = (HttpWebRequest)WebRequest.Create(uri);
        request.Headers.Add("Authorization", "Basic " + Convert.ToBase64String(Encoding.ASCII.GetBytes(_apiKeySid + ":" + _apiKeySecret)));
        request.AllowAutoRedirect = true;
        var responseBody = (await request.GetResponseAsync()).GetResponseStream();

        using (var fs = File.Create(@"D:\test.mp4"))
        {
            responseBody.CopyTo(fs);
        }
like image 178
Mo Sadeghipour Avatar answered Oct 21 '22 10:10

Mo Sadeghipour


302 Found means that the resource that you are looking for has been moved to the different URL. Check the Location Header of the response to see what is the new URL.

302 Found

The HyperText Transfer Protocol (HTTP) 302 Found redirect status response code indicates that the resource requested has been temporarily moved to the URL given by the Location header. A browser redirects to this page but search engines don't update their links to the resource (in 'SEO-speak', it is said that the 'link-juice' is not sent to the new URL).

like image 27
Hooman Bahreini Avatar answered Oct 21 '22 09:10

Hooman Bahreini