使用Telnet发送HTTP GET命令。

huangapple go评论79阅读模式
英文:

Send http GET command from Telnet

问题

我有一个简单的程序:

package main

import (
	"fmt"
	"net/http"
)

func main() {
	http.HandleFunc("/users", UsersHandler)

	fmt.Println("Starting server...")

	http.ListenAndServe(":8181", nil)
}

func UsersHandler(w http.ResponseWriter, r *http.Request) {
	fmt.Println("Users")
}

如果我从浏览器发送一个 GET 命令:http://localhost:8181,我可以看到打印出来的消息是 "Users",但是如果我从 telnet 连接,当我执行以下命令时没有消息被打印出来:

telnet 127.0.0.1 8181
GET /users HTTP/1.1

有任何想法为什么会这样?

英文:

I have this simple program:

package main

import (
	"fmt"
	"net/http"
)

func main() {
	http.HandleFunc("/users", UsersHandler)

	fmt.Println("Starting server...")

	http.ListenAndServe(":8181", nil)
}

func UsersHandler(w http.ResponseWriter, r *http.Request) {
	fmt.Println("Users")
}

If I send a GET command from the browser: http://localhost:8181 I can see the message "Users" printed, but If I connect from telnet no message is printed when I do:

telnet 127.0.0.1 8181
GET /users HTTP/1.1

Any idea why is that ?

答案1

得分: 5

你需要输入第二个回车符,表示头部块的结束。

英文:

You need to enter a second carriage return, which signifies the end of the headers block.

huangapple
  • 本文由 发表于 2014年1月12日 03:37:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/21066916.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定