Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

404的错误 #2

Closed
suntsh opened this issue Aug 12, 2014 · 4 comments
Closed

404的错误 #2

suntsh opened this issue Aug 12, 2014 · 4 comments

Comments

@suntsh
Copy link

suntsh commented Aug 12, 2014

如果访问一个不存在的链接,会不会导致没有body,导致错误呢
// Read response body into string.
func (this *Response) ToString() (string, error) {
defer this.Body.Close()
bytes, err := ioutil.ReadAll(this.Body)
if err != nil {
return "", err
}

return string(bytes), nil

}

@suntsh
Copy link
Author

suntsh commented Aug 12, 2014

测试一下resp.Status != 200时候,是不是导致错误

@ddliu
Copy link
Owner

ddliu commented Aug 12, 2014

resp.Status != 200并不认为是错误,body还是有的,通过ToString也可以取得对应的响应

@suntsh
Copy link
Author

suntsh commented Aug 13, 2014

在什么情况下会没有body,昨天我用了,估计是没有读到body,也没有出现错误,导致后面对body的处理失败,程序退出了

bigbet@126.com

发件人: dong
发送时间: 2014-08-13 05:45
收件人: ddliu/go-httpclient
抄送: suntsh
主题: Re: [go-httpclient] 404的错误 (#2)
resp.Status != 200并不认为是错误,body还是有的,通过ToString也可以取得对应的响应

Reply to this email directly or view it on GitHub.

@ddliu
Copy link
Owner

ddliu commented Aug 13, 2014

res, err := client.Get(url, nil)
if err == nil  {
   // res.Body might be nil
} else {
   fmt.Println(res.ToString())
}

@ddliu ddliu closed this as completed Sep 2, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants