无法通过golang的http.Get()方法获取内容,而是返回500错误。为什么?

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

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.

huangapple
  • 本文由 发表于 2013年6月16日 22:01:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/17134102.html
匿名

发表评论

匿名网友

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

确定