Tono Nam
Tono Nam

Reputation: 36070

Why does web browser makes two requests to download a mp3 file?

I am creating a very simple web server where its only purpose is to return an mp3 file.

// create server
TcpListener server = new TcpListener(8585);
server.Start();

while (true)
{
    // accept a new client
    using (var client = server.AcceptTcpClient())
    {                                        
        byte[] buffer = new byte[1024 * 2];

        var i = client.Client.Receive(buffer);

        // get request client sent us
        var request = System.Text.Encoding.UTF8.GetString(buffer, 0, i).ToLower();

        // if the browser is requesting an icon return
        if (request.Contains("/favicon.ico"))
        {
            continue;
        }

        // with fiddler when I downloaded great.mp3 I saved the response as a binary file to make sure I am returning the right output

        client.Client.Send(System.IO.File.ReadAllBytes("cheat.bin"));

    }
}

cheat.bin can be downloaded from here

it basically consists of

HTTP/1.1 200 OK
Server: Apache/2.2.3 (CentOS)
X-HOST: sipl-web-233.oddcast.com
X-TTSCache: HIT
X-TTSCacheInsert: NO
Content-Type: audio/mpeg
Access-Control-Allow-Origin: *
Vary: Accept-Encoding
Date: Thu, 01 May 2014 00:02:15 GMT
Content-Length: 5471
Connection: keep-alive

ID3���� .... rest of mp3 file!

So my question is why is it that when I go to http://localhost:8585/getSong on my web browser the song is downloaded twice? In other words if I place a breakpoint in my code I hit it twice. Also I will not be able to play the audio in the browser until I return the song a second time.

I am asking this question primarily to learn. I dont understand what is wrong.

Upvotes: 0

Views: 316

Answers (1)

Matthew Lock
Matthew Lock

Reputation: 13556

Browsers often make a HTTP head call before a HTTP get don't they? Could that be why?

https://ochronus.com/http-head-request-good-uses/

Anyway I would use something like Fiddler to see exactly what HTTP requests the browser is making.

Upvotes: 2

Related Questions