英文:
Getting DNS error for curl localhost:8080
问题
我正在尝试在Go语言中创建一个HTTP服务器,当我运行代码时,输出结果是:
监听 localhost:8080
但是当我使用 curl localhost:8080 进行测试时,出现了DNS错误。
以下代码启动一个HTTP服务器,监听8080端口的传入请求。
package main
import (
"fmt"
"html"
"log"
"net/http"
)
func main() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hello, %q", html.EscapeString(r.URL.Path))
})
log.Println("监听 localhost:8080")
log.Fatal(http.ListenAndServe(":8080", nil))
}
这是我运行 curl localhost:8080 时得到的输出:
$ curl localhost:8080
<HTML><HEAD>
<TITLE>网络错误</TITLE>
</HEAD>
<BODY>
<FONT face="Helvetica">
<big><strong></strong></big><BR>
</FONT>
<blockquote>
<TABLE border=0 cellPadding=1 width="80%">
<TR><TD>
<FONT face="Helvetica">
<big>网络错误 (dns_unresolved_hostname)</big>
<BR>
<BR>
</FONT>
</TD></TR>
<TR><TD>
<FONT face="Helvetica">
您请求的主机“localhost”无法通过DNS解析。
</FONT>
</TD></TR>
<TR><TD>
<FONT face="Helvetica">
</FONT>
</TD></TR>
<TR><TD>
<FONT face="Helvetica" SIZE=2>
<BR>
如需帮助,请联系您的网络支持团队。
</FONT>
</TD></TR>
</TABLE>
</blockquote>
</FONT>
</BODY></HTML>
看起来是一个DNS问题,我正在尝试在虚拟机中设置HTTP服务器,如何解决这个错误?
英文:
I'm trying to create a http server in go, when I run the code, I'm getting the output as
Listening on localhost:8080
But when I use curl localhost:8080 to test it I'm getting DNS error
The below code starts an HTTP server, listens on port 8080 incoming requests.
package main
import (
"fmt"
"html"
"log"
"net/http"
)
func main() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hello, %q", html.EscapeString(r.URL.Path))
})
log.Println("Listening on localhost:8080")
log.Fatal(http.ListenAndServe(":8080", nil))
}
This is the output I get when I run curl localhost:8080
$ curl localhost:8080
<HTML><HEAD>
<TITLE>Network Error</TITLE>
</HEAD>
<BODY>
<FONT face="Helvetica">
<big><strong></strong></big><BR>
</FONT>
<blockquote>
<TABLE border=0 cellPadding=1 width="80%">
<TR><TD>
<FONT face="Helvetica">
<big>Network Error (dns_unresolved_hostname)</big>
<BR>
<BR>
</FONT>
</TD></TR>
<TR><TD>
<FONT face="Helvetica">
Your requested host "localhost" could not be resolved by DNS.
</FONT>
</TD></TR>
<TR><TD>
<FONT face="Helvetica">
</FONT>
</TD></TR>
<TR><TD>
<FONT face="Helvetica" SIZE=2>
<BR>
For assistance, contact your network support team.
</FONT>
</TD></TR>
</TABLE>
</blockquote>
</FONT>
</BODY></HTML>
Looks like its an DNS issue, I'm trying to setup the http server in a VM, how can I fix this error?
答案1
得分: 1
一个猜测是你的HTTP请求经过了代理。你可以取消设置HTTP_PROXY
变量,然后再试一次。
否则,尝试使用127.0.0.1
或你的机器的IP地址。:8080
表示HTTP服务器绑定到所有IP地址。
英文:
One guess is your http request is going through a proxy. You can unset the HTTP_PROXY
variable and try again.
Otherwise, try using 127.0.0.1
or the IP address for your machine. :8080
means the HTTP server is binding to all IPs.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论