注意:使用的是ArduinoHttpClient,与ESP32内部的HTTPClient库有较大的差别
- 海康摄像机http接入时候,由于没有认证信息,会返回401无权限信息,这时需要进行digest认证;
- 首次请求后返回digest认证信息,使用字符处理提取出qop、realm、nonce这几个关键字
- 然后就需要进行digest加密了,这个时候可以参考如下几篇博文确认加密原理
- http Digest认证计算方法整理_TYINY的博客-CSDN博客_digest 算法
- http://web.chacuo.net/safedigestauth
- md5加密,sha1加密--md5在线解密
- 通过ESP内部的MD5库进行加密,将认证信息放入到header中再次请求,即可实现摄像机的连接
#include <Arduino.h>
#include <HttpClient.h>
#include <WiFi.h>
#include <WiFiMulti.h>
#include "MD5Builder.h"
char ssid[] = "xxxxx"; //按需调整
char pass[] = "xxxxx"; //按需调整
WiFiClient wifi;
HttpClient client = HttpClient(wifi, "10.17.97.117", 80); //IP为摄像机的IP,80为http的端口
void setup() {
Serial.begin(115200);
Serial.println();
Serial.println();
Serial.println();
//wifi连接
WiFi.begin(ssid, pass);
while ( WiFi.status() != WL_CONNECTED) {
Serial.print("Attempting to connect to Network named: ");
Serial.println(ssid); // print the network name (SSID);
// Connect to WPA/WPA2 network:
sleep(2);
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void loop() {
String devAuthStr;
String retAuthStr;
String response;
String qop;
String realm;
String nonce;
String nc="00000001";
String cnonce = "f66f3849aa2540cb";
String stra1,stra2;
String strUri = "/ISAPI/System/deviceInfo";
Serial.print("[HTTP] begin...\n");
client.get("/ISAPI/System/deviceInfo");
// read the status code and body of the response
int statusCode = client.responseStatusCode();
Serial.printf("[HTTP] GET... code: %d\n", statusCode);
// httpCode will be negative on error
if(statusCode == 200)
{
String response = client.responseBody();
Serial.print("Response: ");
Serial.println(response);
}
else if(401 == statusCode) //需要进行digest认证
{
// from header get auth info
while(client.headerAvailable())
{
if(0 == client.readHeaderName().compareTo("WWW-Authenticate"))
{
devAuthStr = client.readHeaderValue();
break;
}
}
Serial.println(devAuthStr);
int index1=0;
int index2=0;
index1 = devAuthStr.indexOf("qop");
index2 = devAuthStr.indexOf(",", index1);
qop = devAuthStr.substring(index1+5, index2-1);
index1 = devAuthStr.indexOf("realm");
index2 = devAuthStr.indexOf(",", index1);
realm = devAuthStr.substring(index1+7, index2-1);
index1 = devAuthStr.indexOf("nonce");
index2 = devAuthStr.indexOf(",", index1);
nonce = devAuthStr.substring(index1+7, index2-1);
Serial.println(qop);
Serial.println(realm);
Serial.println(nonce);
MD5Builder md5;
md5.begin();
md5.add("admin:");
md5.add(realm);
md5.add(":abcd1234");
md5.calculate();
stra1 = md5.toString();
md5.begin();
md5.add("GET:");
md5.add(strUri);
md5.calculate();
stra2 = md5.toString();
md5.begin();
md5.add(stra1);
md5.add(":");
md5.add(nonce);
md5.add(":");
md5.add(nc);
md5.add(":");
md5.add(cnonce);
md5.add(":");
md5.add(qop);
md5.add(":");
md5.add(stra2);
md5.calculate();
response = md5.toString();
retAuthStr = "Authorization: Digest username=\"admin\", realm=\""+realm+"\", nonce=\""+nonce+"\", uri=\""+strUri+"\", response=\""+response+"\", qop=\""+qop+"\", nc="+nc+", cnonce=\""+cnonce+"\"";
Serial.println(retAuthStr);
client.beginRequest();
client.get(strUri);
client.sendHeader(retAuthStr);
client.endRequest();
statusCode = client.responseStatusCode();
Serial.printf("[HTTP] GET... code: %d\n", statusCode);
String responseRes = client.responseBody();
Serial.print(responseRes);
}
else
{
Serial.printf("[ERROR][HTTP] GET... code: %d, can not process!\n", statusCode);
}
Serial.println();
Serial.println();
Serial.println();
delay(60000);
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)