获取 curl localhost:8080 时出现 DNS 错误。

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

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 (
    &quot;fmt&quot;
    &quot;html&quot;
    &quot;log&quot;
    &quot;net/http&quot;
) 

func main() {

    http.HandleFunc(&quot;/&quot;, func(w http.ResponseWriter, r *http.Request) {
        fmt.Fprintf(w, &quot;Hello, %q&quot;, html.EscapeString(r.URL.Path))
    })

    log.Println(&quot;Listening on localhost:8080&quot;)

    log.Fatal(http.ListenAndServe(&quot;:8080&quot;, nil))
}

This is the output I get when I run curl localhost:8080

$ curl localhost:8080
&lt;HTML&gt;&lt;HEAD&gt;
&lt;TITLE&gt;Network Error&lt;/TITLE&gt;
&lt;/HEAD&gt;
&lt;BODY&gt;
&lt;FONT face=&quot;Helvetica&quot;&gt;
&lt;big&gt;&lt;strong&gt;&lt;/strong&gt;&lt;/big&gt;&lt;BR&gt;
&lt;/FONT&gt;
&lt;blockquote&gt;
&lt;TABLE border=0 cellPadding=1 width=&quot;80%&quot;&gt;
&lt;TR&gt;&lt;TD&gt;
&lt;FONT face=&quot;Helvetica&quot;&gt;
&lt;big&gt;Network Error (dns_unresolved_hostname)&lt;/big&gt;
&lt;BR&gt;
&lt;BR&gt;
&lt;/FONT&gt;
&lt;/TD&gt;&lt;/TR&gt;
&lt;TR&gt;&lt;TD&gt;
&lt;FONT face=&quot;Helvetica&quot;&gt;
Your requested host &quot;localhost&quot; could not be resolved by DNS.
&lt;/FONT&gt;
&lt;/TD&gt;&lt;/TR&gt;
&lt;TR&gt;&lt;TD&gt;
&lt;FONT face=&quot;Helvetica&quot;&gt;

&lt;/FONT&gt;
&lt;/TD&gt;&lt;/TR&gt;
&lt;TR&gt;&lt;TD&gt;
&lt;FONT face=&quot;Helvetica&quot; SIZE=2&gt;
&lt;BR&gt;
For assistance, contact your network support team.
&lt;/FONT&gt;
&lt;/TD&gt;&lt;/TR&gt;
&lt;/TABLE&gt;
&lt;/blockquote&gt;
&lt;/FONT&gt;
&lt;/BODY&gt;&lt;/HTML&gt;

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.

huangapple
  • 本文由 发表于 2022年11月3日 04:12:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/74295150.html
匿名

发表评论

匿名网友

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

确定