Golang 403禁止错误 [英] Golang 403 forbidden error

查看:104
本文介绍了Golang 403禁止错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试检查本地主机时,它返回正确的状态,但是当我尝试在网络上轮询计算机时,它显示403-Forbidden错误.

When I try to check a local host it returns the correct status but when I am trying to poll a machine on the network it shows 403-Forbidden error.

package main

import "net/http"
import "fmt"

func main() {
    resp, err := http.Get("http://site-centos-64:8080/examples/abc1.jsp")
    fmt.Println(resp,err)
}

推荐答案

在不知道您正在运行的确切设置的情况下,我只能猜测,但这通常在Web服务器对请求标头进行过滤时发生.您可能需要添加一个 Accept 和一个 User-Agent 标头,以允许请求.

Without knowing the exact setup you're running, I can only guess, but this usually happens when the web server is filtering on request headers. You may need to add anAccept and a User-Agent header for the request to be allowed.

尝试类似的东西:

curl -i \
-H 'Accept:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8' \
-H 'User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_5) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11' \
http://site-centos-64:8080/examples/abc1.jsp

如果可行,则可以使用标头在Go代码中设置这些标头.Add()方法.

If this works, you can set these headers in the Go code using the Header.Add() method.

package main

import "net/http"
import "fmt"

func main() {
    client := &http.Client{
        CheckRedirect: redirectPolicyFunc,
    }
    req, err := http.NewRequest("GET", "http://site-centos-64:8080/examples/abc1.jsp", nil)
    req.Header.Add("Accept", `text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8`)
    req.Header.Add("User-Agent", `Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_5) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11`)
    resp, err := client.Do(req)
    fmt.Println(resp,err)
}

上面的curl命令从答案中修改为 CURL HTTP/1.1 403禁止日期.

The curl command above is modified from the answer to CURL HTTP/1.1 403 Forbidden Date .

这篇关于Golang 403禁止错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆