how to use netcat (nc) to test HTTP url redirection

Netcat (nc) is pretty powerful command line tool on Linux & Mac and can be used to send data on network. It can be quickly used to communicate with any webserver with custom headers. These headers can have custom url, Host, User-agent, etc. Here are some HTTP url redirection scenarios which can be tested with netcat.

Test basic redirection

To test if /feed/ redirects to feedburner url run this on command line (Mac or Linux):

printf "GET /feed/ HTTP/1.1\nHost:infoheap.com\nUser-Agent:Firefox\n\n" | nc infoheap.com 80

Here expected outcome is a HTTP 302 redirect to http://feeds.infoheap.com/infoheap. Here is the initial part of the outcome on Mac:

HTTP/1.1 302 Found
Date: Wed, 01 May 2013 15:36:38 GMT
Server: Apache/2.2.22 (Ubuntu)
Location: http://feeds.infoheap.com/infoheap
Vary: Accept-Encoding
Content-Length: 296
Content-Type: text/html; charset=iso-8859-1

Test Host based redirection

To test if http://www.infoheap.com/ redirects to http://infoheap.com/, run this on command line:

printf "GET / HTTP/1.1\nHost:www.infoheap.com\nUser-Agent:Firefox\n\n" | nc infoheap.com 80

Here expected outcome is a HTTP 301 redirect to http://infoheap.com/. Here is initial part of the real outcome on Mac:

HTTP/1.1 301 Moved Permanently
Date: Wed, 01 May 2013 15:43:02 GMT
Server: Apache/2.2.22 (Ubuntu)
Location: http://infoheap.com/
Vary: Accept-Encoding
Content-Length: 310
Content-Type: text/html; charset=iso-8859-1

Test user-agent based redirection

To test if /feed/ url is not redirected for User-Agent=Feedburner, run this on command line:

printf "GET /feed/ HTTP/1.1\nHost:infoheap.com\nUser-Agent:Feedburner\n\n" | nc infoheap.com 80

Here expected outcome is no redirect and xml content should be served. Here is initial part of the real outcome on Mac:

HTTP/1.1 200 OK
Date: Wed, 01 May 2013 15:46:04 GMT
Server: Apache/2.2.22 (Ubuntu)
X-Powered-By: PHP/5.3.10-1ubuntu3.6
Vary: Accept-Encoding
X-Pingback: http://infoheap.com/xmlrpc.php
Last-Modified: Wed, 01 May 2013 12:52:39 GMT
Transfer-Encoding: chunked
Content-Type: text/xml; charset=UTF-8

 

Netcat can be used in many such scenarios to test if redirection, etc. is working fine. It can be pretty useful and time saver tool once we get comfortable with it.

Share this article: share on Google+ share on facebook share on linkedin tweet this submit to reddit

Comments

Click here to write/view comments