1 回答

TA貢獻(xiàn)1839條經(jīng)驗(yàn) 獲得超15個(gè)贊
有幾種選擇。第一種是返回錯(cuò)誤響應(yīng):
func handle(w http.ResponseWriter, r *http.Request) {
if r.UserAgent() == "test/1.0" {
//Allow
} else {
http.Error(w, "forbidden", http.StatusForbidden)
return
}
}
第二種是從服務(wù)器劫持連接并粗魯?shù)仃P(guān)閉連接:
func handle(w http.ResponseWriter, r *http.Request) {
if r.UserAgent() == "test/1.0" {
//Allow
} else {
if h, ok := w.(http.Hijacker); ok {
c, _, err := h.Hijack()
if err != nil {
c.Close()
return
}
}
// fallback to error response
http.Error(w, "forbidden", http.StatusForbidden)
return
}
}
- 1 回答
- 0 關(guān)注
- 95 瀏覽
添加回答
舉報(bào)