I'm testing the following code with a YouTube video and it hangs when it gets to the Read function ...it doesn't seem to receive any data and after a while the socket disconnects and the app terminates 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 that what is failing is the request because I have tested the rest of the code on my local server and it works correctly (although it is also true that it was not with streaming).
Does anyone know how to make 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 bad request error...
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 in the chrome developer 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 keep getting ERROR 400!!
Any ideas? Is there something missing or extra? Any syntax errors?
It's weird because I've seen on some websites that it can be done easily with a much simpler header... (these two for example)
https://www.askapache.com/online-tools/http-headers-tool
They get the HTML of the video page, which I'm interested in reading some interesting facts... but my ultimate goal is to be able to capture the streaming video. If you can guide me a bit with that, that would be great too!
What happens is that the header you send is missing information. The server waits a reasonable time for the missing information to arrive and... since that doesn't happen... it cuts the connection.
The header you send should look more like this:
And taking great care of the line breaks... I recommend you read the HTTP protocol specification to understand more about it.