关于Onvif协议获得快照需要身份验证问题

关于Onvif协议获得快照需要身份验证问题


大家在通过onvif协议获得摄像头快照的时候会获得一个网络访问地址,在把地址输入到浏览器访问会出现身份验证问题:
在这里插入图片描述

在网也输入用户名密码才可以访问,但是想通过代码获得快照就需要在代码中加入身份验证。通过网上查询大多数都是教大家设置basic验证的。如图:
在这里插入图片描述
但是你会发现你加入这个后还是返回401,这是因为有的摄像头采用的是其他身份验证方式,如乐橙采用的是digest身份验证 。下面以go语言为例。其他语言digest身份验证大家自行查询:

func DigestParts(resp *http.Response) map[string]string {
	result := map[string]string{}
	if len(resp.Header["Www-Authenticate"]) > 0 {
		wantedHeaders := []string{"nonce", "realm", "qop"}
		responseHeaders := strings.Split(resp.Header["Www-Authenticate"][0], ",")
		for _, r := range responseHeaders {
			for _, w := range wantedHeaders {
				if strings.Contains(r, w) {
					result[w] = strings.Split(r, `"`)[1]
				}
			}
		}
	}
	return result
}

func getMD5(text string) string {
	hasher := md5.New()
	hasher.Write([]byte(text))
	return hex.EncodeToString(hasher.Sum(nil))
}

func getCnonce() string {
	b := make([]byte, 8)
	io.ReadFull(rand.Reader, b)
	return fmt.Sprintf("%x", b)[:16]
}

func GetDigestAuthrization(digestParts map[string]string) string {
	d := digestParts
	ha1 := getMD5(d["username"] + ":" + d["realm"] + ":" + d["password"])
	ha2 := getMD5(d["method"] + ":" + d["uri"])
	nonceCount := 00000001
	cnonce := getCnonce()
	response := getMD5(fmt.Sprintf("%s:%s:%v:%s:%s:%s", ha1, d["nonce"], nonceCount, cnonce, d["qop"], ha2))
	authorization := fmt.Sprintf(`Digest username="%s", realm="%s", nonce="%s", uri="%s", cnonce="%s", nc="%v", qop="%s", response="%s"`,
		d["username"], d["realm"], d["nonce"], d["uri"], cnonce, nonceCount, d["qop"], response)
	return authorization
}

使用:

//网络请求获取快照
	client := &http.Client{}
	req, err := http.NewRequest("GET", url, nil)
	if err != nil {
		fmt.Println(req)
	}
	resp, err := client.Do(req)
	//加入digest身份验证
	digestParts := digestauth.DigestParts(resp)
	digestParts["uri"] = req.URL.Path
	digestParts["username"] = OD.UserName
	digestParts["password"] = OD.Password
	digestParts["method"] = "GET"
	req.Header.Set("Authorization", digestauth.GetDigestAuthrization(digestParts))
	resp1,_:=client.Do(req)
	//读取返回的图片流
	imageStream,err :=ioutil.ReadAll(resp1.Body)
	if err != nil {
		fmt.Println("imageStream err:",err)
	}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值