英文:
Can't get the content by golang http.Get(), 500 error instead. Why?
问题
我无法获取一个网站的内容,它返回500错误。但是如果我切换到www.google.com.hk或其他网站,就可以了。为什么?
以下是代码。
package main
import (
"fmt"
"io/ioutil"
"log"
"net/http"
)
func main() {
resp, err := http.Get("http://www.eqsn.gov.cn") //浏览器,IE\firefox访问它是可以的。
// resp, err := http.Get("http://www.google.com.hk") //可以。
if err != nil {
log.Fatalf("http.Get => %v", err.Error())
}
body, _ := ioutil.ReadAll(resp.Body)
fmt.Printf("\n%v\n\n", string(body))
}
英文:
I can't get a site's content, it returns the 500 error. But if I switch to www.google.com.hk or other sites, it's OK. Why?
The following is the code.
package main
import (
"fmt"
"io/ioutil"
"log"
"net/http"
)
func main() {
resp, err := http.Get("http://www.eqsn.gov.cn") //the browsers,IE\firefox access it is ok.
// resp, err := http.Get("http://www.google.com.hk") //It's ok.
if err != nil {
log.Fatalf("http.Get => %v", err.Error())
}
body, _ := ioutil.ReadAll(resp.Body)
fmt.Printf("\n%v\n\n", string(body))
}
答案1
得分: 4
如果http.Get()
的执行返回错误500(内部服务器错误),很可能这个错误来自服务器。实际上,让我们手动尝试一下。选项-D-
会转储头信息。
$ curl -D- http://www.eqsn.gov.cnHTTP/1.0 500 Internal Server Error
Date: Mon, 17 Jun 2013 02:01:11 GMT
Content-Type: text/html; charset=iso-8859-1
Content-Length: 538
X-Powered-By:
X-AspNet-Version:
MicrosoftOfficeWebServer:
Server:
X-Cache: MISS from CNC-JSWX-254-131.fastcdn.com
X-Cache: MISS from CT-ZJNB-152-196.fastcdn.com
Connection: close
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>500 Internal Server Error</title>
</head><body>
<h1>Internal Server Error</h1>
<p>服务器遇到了内部错误或配置错误,无法完成您的请求。</p>
<p>请联系服务器管理员,[未提供地址],并告知他们错误发生的时间以及您可能做过的任何可能导致错误的操作。</p>
<p>有关此错误的更多信息可能在服务器错误日志中提供。</p>
</body></html>
正如您所见,服务器给出了错误500。Go工作得非常好;它给出了服务器发送的错误500。如果您有进一步的问题,请随时提问。
英文:
If an execution of http.Get()
returns an error 500 (internal server error), it is pretty likely that this error comes from the server. In fact, let's try manually. The option -D-
dumps the headers.
$ curl -D- http://www.eqsn.gov.cnHTTP/1.0 500 Internal Server Error
Date: Mon, 17 Jun 2013 02:01:11 GMT
Content-Type: text/html; charset=iso-8859-1
Content-Length: 538
X-Powered-By:
X-AspNet-Version:
MicrosoftOfficeWebServer:
Server:
X-Cache: MISS from CNC-JSWX-254-131.fastcdn.com
X-Cache: MISS from CT-ZJNB-152-196.fastcdn.com
Connection: close
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>500 Internal Server Error</title>
</head><body>
<h1>Internal Server Error</h1>
<p>The server encountered an internal error or
misconfiguration and was unable to complete
your request.</p>
<p>Please contact the server administrator,
[no address given] and inform them of the time the error occurred,
and anything you might have done that may have
caused the error.</p>
<p>More information about this error may be available
in the server error log.</p>
</body></html>
As you can see, the server gives you an error 500. Go works completely fine; it gives you the error 500 the server sends. If you have further questions, feel free to ask.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论