telnetで自分のwebサーバと通信する方法

telnet コマンドでlocalhostの80番ポートにつなげる。(localhostでなくドメイン名を指定するとルータの管理画面などが表示されてしまう。)


[user@example ~]$ telnet localhost 80
Trying ::1...
Connected to localhost.
Escape character is '^]'.
GET /index.html HTTP/1.0
Host: localhost <= リクエストを入力したらEnterキーを2回押す

HTTP/1.1 200 OK
Date: Wed, 25 Dec 2013 10:01:49 GMT

ちなみにHTTPプロトコルのバージョンをHTTP/1.1にしたらエラーになった。

[user@example ~]$ telnet localhost 80
Trying ::1...
Connected to localhost.
Escape character is '^]'.
GET /index.html HTTP/1.1

HTTP/1.1 400 Bad Request
Date: Wed, 25 Dec 2013 10:08:10 GMT

↑削除しました。原因はHostヘッダを指定しなかったことです。HTTP/1.1でリクエストを送るときはHostヘッダの指定は必須になります。

以上。

Leave a Reply

Your email address will not be published. Required fields are marked *