shortcutmania.com

Using telnet to troubleshoot a web server


Telnet can be quite useful to troubleshoot a web server and it is superior to a web browser for several reasons:

  • telnet client availability is pretty much universal.
  • a web browser might fail for many reasons that are not related to your web server
  • a web browser might re-format the response given by the web server to the point that it is no longer clear what the problem is.
  • you want to see the raw data returned by the web server

If you are using the Microsoft Telnet Client you will not see what you type by default. To turn on the local display follow the instructions from this article: Microsoft Telnet Client tips and tricks

A few notes on the HTTP protocol

The general format of an HTTP 1.1 request looks like this:

[command] [path] HTTP/1.1
Host: www.yourdomain.com
<optional headers>

command - HEAD, GET, POST or some other command understood by your web server. The HEAD command will only return the header information for the requested page while GET and POST will return body data as well.
path - the web page targeted by this command. The path should not include the host name. This value must be specified, at a minimum you must use "/".
optional headers - other HTTP headers, one per line.

To signal the web server that you completed the command press Enter on the last empty line.

 

How to connect to your web server with telnet

Open a command prompt and type

telnet www.yourdomain.com 80

If you are greeted with an empty page the connection was successful. In case of an error the error message is display and telnet exists.

 

How to check if a web server is responding on port 80

After the telnet connection is established (connect as described above) type

HEAD / HTTP/1.1
Host: www.yourdomain.com

You should receive a response similar to this one:

HTTP/1.1 200 OK
Content-Length: 7343
Content-Type: text/html
Content-Location: http://www.tbiro.com/index.htm
Last-Modified: Wed, 24 Dec 2008 05:32:39 GMT
Accept-Ranges: bytes
ETag: "28e04098965c91:25681"
Server: Microsoft-IIS/6.0
X-Powered-By: ASP.NET

The above response tells us a lot of information about the requested page as well as the server that hosts this page. The first line contains the HTTP status returned by the server, 200 means that there were no errors.

 

How to get a web page.

After the telnet connection is established (connect as described above) type

GET / HTTP/1.1
Host: www.yourdomain.com

The resulting data might be big so it might help to turn on telnet client logging as described in the Microsoft Telnet Client tips and tricks

You can also do a POST but in this case you also need to specify the content length.

POST /yourpage.asp HTTP/1.1
Host: www.yourdomain.com
Content-Length: [postdatalength]

[postdata]

Make sure to replace the [postdatalength] with the actual length of your posted data and [postdata] with your posted data. Also notice the empty line right after the Content-length header, this is required and it signals the web server that the posted data follows.