How to get a web page's content using Telnet?
For example, the content of https://stackoverflow.com/questions
.
Join Stack Overflow to learn, share knowledge, and build your career.
telnet ServerName 80
GET /index.html↵
↵
↵ means 'return', you need to hit return twice
GET /index.html
, otherwise, it wouldn't work. The end of the header section is indicated by an empty field(line), resulting in the transmission of two consecutive CR-LF pairs. — from Wikipedia
– Nikolay Konovalov
Nov 8 '18 at 21:52
{ echo "GET /"; sleep 1; } | telnet localhost 80
(from stackoverflow.com/questions/7013137/…)
– baptx
Jun 15 '19 at 11:56
You could do
telnet stackoverflow.com 80
And then paste
GET /questions HTTP/1.0
Host: stackoverflow.com
# add the 2 empty lines above but not this one
Here is a transcript
$ telnet stackoverflow.com 80
Trying 151.101.65.69...
Connected to stackoverflow.com.
Escape character is '^]'.
GET /questions HTTP/1.0
Host: stackoverflow.com
HTTP/1.1 200 OK
Content-Type: text/html; charset=utf-8
...
HTTP/1.1 400 Bad Request
response. I fixed it and included a transcript. Cheers.
– Bruno Bronosky
Apr 24 '17 at 23:53
For posterity, your question was how to send an http request to https://stackoverflow.com/questions
. The real answer is: you cannot with telnet, cause this is an https-only reachable url.
So, you might want to use openssl
instead of telnet
, like this for instance
$ openssl s_client -connect stackoverflow.com:443
...
---
GET /questions HTTP/1.1
Host: stackoverflow.com
This will give you the https response.
To somewhat expand on earlier answers, there are a few complications.
telnet
is not particularly scriptable; you might prefer to use nc
(aka netcat
) instead, which handles non-terminal input and signals better.
Also, unlike telnet
, nc
actually allows SSL (and so https
instead of http
traffic -- you need port 443 instead of port 80 then).
There is a difference between HTTP 1.0 and 1.1. The recent version of the protocol requires the Host:
header to be included in the request on a separate line after the POST
or GET
line, and to be followed by an empty line to mark the end of the request headers.
The HTTP protocol requires carriage return / line feed line endings. Many servers are lenient about this, but some are not. You might want to use
printf "%\r\n" \
"GET /questions HTTP/1.1" \
"Host: stackoverflow.com" \
"" |
nc --ssl stackoverflow.com 443
If you fall back to HTTP/1.0 you don't always need the Host:
header, but many modern servers require the header anyway; if multiple sites are hosted on the same IP address, the server doesn't know from GET /foo HTTP/1.0
whether you mean http://site1.example.com/foo
or http://site2.example.net/foo
if those two sites are both hosted on the same server (in the absence of a Host:
header, a HTTP 1.0 server might just default to a different site than the one you want, so you don't get the contents you wanted).
The HTTPS protocol is identical to HTTP in these details; the only real difference is in how the session is set up initially.
{ echo "GET / HTTP/1.1"; echo "Host: example.com"; echo; sleep 1; } | ncat --ssl example.com 443
– baptx
Jun 15 '19 at 12:29
printf
. Maybe the sleep
helps in some situations.
– tripleee
Jun 15 '19 at 17:41
printf
can do more things but echo
is enough in this case. I just thought it can be useful to share a one-liner with netcat.
– baptx
Jun 24 '19 at 9:50
ncat
instead, because nc
doesn't provide --ssl
.
– Niloct
Feb 19 at 0:21
printf
version can certainly be folded to a single line simply by removing the newlines, too.
– tripleee
Feb 19 at 6:22
telnet telehack.com
. List of examples – Lime Sep 1 '15 at 19:24