Stuck reading YouTube video with C ++ and wxWidgets

2

I am testing the following code with a YouTube video and it stays on hold when it reaches the Read function ... it seems that it does not receive any data and after a while the socket disconnect and the application ends normally.

int MainApp::OnRun()
{
    wxIPV4address addr;

    // address to connect
    wxString host = wxT("www.youtube.com");
    addr.Hostname(host);
    int port = 80;


    addr.Service(port);

    wxSocketClient *socket = new wxSocketClient();

    print(wxT("Connecting..."));

    socket->Connect(addr);

    if (socket->IsConnected())
        print(wxT("Connected to ") + addr.IPAddress());
    else
    {
        print(wxT("Can not connect to ") + host);
        return 1;
    } 

   **wxString request = wxT("GET /watch?v=NAsCGnXJ2cg HTTP/2.0\r\n");**


    // send the request to the server
    socket->Write(request.mb_str(), request.Length());
    print(wxT("Request sent"));


    char c = 0x00;
    wxString data;

    print(wxT("Receiving data..."));

    while( socket->IsConnected() && !socket->Error() )
    {
        // read a char
        socket->Read(&c, 1);

        // append char to string
        data.Append((wxChar)c, 1);

        cout<<c;
    }

    delete socket;

    // print received data
    print(data);
    return 0;
}

I think what is failing is the request because I've tried the rest of the code on my local server and it works correctly (although it's also true that it was not streaming).

Does anyone know how to do the request correctly for YouTube to send me the video data?

  

OK, following the instructions of the person who answered me I made the request in this way.

wxString request = wxT ("GET / watch? v = NAsCGnXJ2cg HTTP / 2.0 \ r \ n"

                      "Host: www.youtube.com\r\n"

                      "User-Agent: Chrome/63.0.3239.132\r\n"

                      "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n"

                      "Content-Length: 0\r\n"

                      "Content-Type: text/plain;charset=UTF-8\r\n"

                      "\r\n"
                      );

But I have the 400 error of bad request ...

  

HTTP / 1.0 400 Bad Request

     

Content-Type: text / html; charset = UTF-8

     

Referrer-Policy: no-referrer

     

Content-Length: 1555

     

Date: Thu, 12 Apr 2018 07:47:44 GM

I've been looking at the Chrome development tools and I've seen this

  

: authority: www.youtube.com

     

: method: GET

     

: path: / watch? v = rg9Dv1no_9c

     

: scheme: https accept:

     

text / html, application / xhtml + xml, application / xml; q = 0.9, image / webp, image / apng, / ; q = 0.8

     

accept-encoding: gzip, deflate, br

     

accept-language: es-ES, es; q = 0.9

     

cache-control: max-age = 0

     

cookie: ...............

     

upgrade-insecure-requests: 1

     

user-agent: Mozilla / 5.0 (Windows NT 6.3; Win64; x64) AppleWebKit / 537.36 (KHTML, like Gecko) Chrome / 65.0.3325.181 Safari / 537.36

     

x-client-data: .............

And based on that I have put together the following petition

wxString request2 = wxT("GET /watch?v=rg9Dv1no_9c HTTP/2.0\r\n"
                        "Host: https://www.youtube.com\r\n"
                        "Content-Type: text/html;charset=UTF-8\r\n"
                        "Accept-Encoding: gzip, deflate, br\r\n"
                        "Accept-Charset: ISO-8859-1,UTF-8;q=0.7,*;q=0.7\r\n"
                        "Connection: keep-alive\r\n"
                        "User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 YaBrowser/18.2.0.284 Yowser/2.5 Safari/537.36\r\n"                            
                        "Cache-Control: max-age=0\r\n"
                        "Accept-Language: es-ES,es;q=0.9\r\n"
                        "Accept-Header: text/html, application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n"
                        "Upgrade-Insecure-Requests: 1\r\n" 
                         "Cookie: CONSENT=YES+ES.es+20150705-15-0; VISITOR_INFO1_LIVE=gW8EMr6HkB8; _ga=GA1.2.720111899.1436744695; endscreen-metadata-editor-gh=true; PREF=f6=42018&f5=30030&al=es&cvdm=grid&f1=50000000; SID=-gUT0RoqrW6iN-QgLD7uolElo-VW_2haDglvrB4reFtVFwk_pugPpzBzW1osmY5Gi_dS-g.; HSID=A7Yc2Drrdyl-yTKod; SSID=AMJGIbMZN1aWbu7EX; APISID=8eu4DiCJED9s0knC/A0bf8LsHROKIBE5UL; SAPISID=YLeTesbQP5mHdfD7/AuDiu6Z73WjgfW7Kv; LOGIN_INFO=AFmmF2swRAIgXfZGlAUHfvxB9mNWhpjTjVvH_k9DPNuS-mv9x6PZ4EMCIEuiOPCnkmUQ5JGgSl7TTNvrSJ2izUUDUOsnYB2VQZ_R:QUQ3MjNmemdfdUxtLTRRd3pCZ3lRNFQ3c1dDcjhsNEhuR2NzMjlPdEJkMVlUTTZycUM4Zm02ZERkV3AydW1PNDRJN0YyR0x4VHdLekw2ektFSi01M1ljM3ljNHJkaURVNkV6a3JncEJwTktubUlOOUdLRE5wdXlSM0VaVVJPV0p1aFNQOXRORWhiMWdYSVBCTmVIeTdNX3F4STBoUzlCNVVfd0lOZ0JUQk93M01IQmxhQktack80; YSC=JzQQe4Eiqfs\r\n"

                        "\r\n"

                        );

But I still get the ERROR 400 !!

Any ideas, is there something missing or something wrong, any errors in the syntax?

It's weird because I've seen some websites that can be done easily with a much simpler header ... (these two for example)

link

link

They get the HTML code of the video page, which interests me to read some interesting data ... but my ultimate goal is to be able to capture the streaming of the video. If you can guide me a bit with that, it would be great too!

    
asked by Iván Rodríguez 11.04.2018 в 07:17
source

1 answer

1

What happens is that the header you send is missing information. The server is waiting a reasonable amount of time for the missing information to arrive and ... as that does not happen ... it cuts the connection.

The header you send should look more like this:

GET /watch?v=NAsCGnXJ2cg HTTP/2.0
Host: www.youtube.com
User-Agent: (Como quieras identificar a tu cliente)
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Content-Length: 0
Content-Type: text/plain;charset=UTF-8

And taking very good care of the line breaks ... I recommend you read the HTTP protocol specification to understand more on the subject.

    
answered by 11.04.2018 / 11:58
source