/******************************************************************
*Company: http://www.xiaomutech.com/
*fileName : qisapithread.cpp --- QIsapiThread
*Auth : yhni (QQ:393320854)
*Create : 2022/6/7
*Description :
*Histroy:
*<Auth> <Date> <Ver> <Content>
* 2022/6/7
*******************************************************************/
#include "qisapithread.h"
#include <QRegExp>
#include <QTcpSocket>
#include <QDebug>
#include <QCryptographicHash>
#include <QEventLoop>
#define DLT_HOT_PIC_POST_PARAM "{\n" \
"\t\"JpegPicWithAppendDataParam\":\n" \
"\t{\n" \
"\t\t\"captureMode\":\"standard\"\n" \
"\t}\n" \
"}\n"
QIsapiThread::QIsapiThread(QObject *parent) : QThread(parent)
{
m_isRun = false;
}
QIsapiThread::~QIsapiThread()
{
}
void QIsapiThread::setHttpAuth(QHttpAuth _auth)
{
m_auth = _auth;
QString result = m_auth.getHttpRequest(true);
emit showResult(result);
}
#if 1
void QIsapiThread::run()
{
QTcpSocket _tcpsocket;
int check = 0;
do {
_tcpsocket.connectToHost(m_auth.host(), 80);
_tcpsocket.waitForConnected(3000);
check++;
} while (check < 3 && QTcpSocket::ConnectedState != _tcpsocket.state());
if (QTcpSocket::ConnectedState != _tcpsocket.state())
{
qDebug() << "Connect server timeout";
return;
}
check = 0;
QByteArray _cmd;
do {
_cmd = m_auth.getHttpRequest(0 != check);
_tcpsocket.write(_cmd);
QByteArray byContent;
int bytes = 0;
do {
_tcpsocket.waitForReadyRead(1000);
QByteArray readBuff;
readBuff.resize(2048);
bytes = _tcpsocket.read(readBuff.data(), readBuff.length());
if (bytes > 0) {
byContent += readBuff.left(bytes);
}
} while (bytes > 0);
if (0 == check){
m_auth.parseResponse(byContent);
} else {
int statusCode = 0;
QRegExp regExp("HTTP/1.1 (\\d+) ");
if (-1 != regExp.indexIn(byContent.left(20)))
{
statusCode = regExp.cap(1).toInt();
}
if (200 == statusCode)
{
int index = byContent.lastIndexOf("\r\n\r\n");
QByteArray httpHeader = byContent.left(index + 4);
int start = httpHeader.lastIndexOf("Content-Length");
int len = httpHeader.mid(start + 15, index - start - 15).toInt();
QByteArray _byData = byContent.mid(index + 4, len);
emit signalContent("image/jpeg", _byData);
}
else {
qDebug() << "Error" << statusCode << byContent;
}
}
check ++;
} while (check < 2);
_tcpsocket.close();
}
#else
void QIsapiThread::run()
{
QNetworkAccessManager http;
QNetworkReply *_reply;
QNetworkRequest request;
int check = 0;
do {
m_auth.setHttpRequest(&request, 0 != check);
if ("POST" == m_auth.method())
{
_reply = http.post(request, m_auth.content());
qDebug() << "====> POST" << m_auth.content();
}
else {
_reply = http.get(request);
}
QEventLoop eventLoop;
connect(_reply, SIGNAL(finished()), &eventLoop, SLOT(quit()));
eventLoop.exec();
int stateCode = _reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
qDebug() << "stateCode" << stateCode ;
if (_reply->hasRawHeader("WWW-Authenticate") && stateCode == 401) {
qDebug() << _reply->rawHeader("WWW-Authenticate");
QString strAuth = _reply->rawHeader("WWW-Authenticate");
m_auth.parseResponse(strAuth);
}
else if (200 == stateCode) {
QString strType = _reply->rawHeader("Content-Type");
int len = _reply->rawHeader("Content-Length").toInt();
QByteArray _byData = _reply->readAll();
qDebug() << strType << len << _byData.length();
emit signalContent(strType, _byData);
}
else {
qDebug() << _reply->readAll();
}
} while (check++ < 1);
}
#endif
//////////////////////////////////////////////////////////////////////////////////////////////////////
QHttpAuth::QHttpAuth(QString host, QString uri, QString method, QString user, QString passwd) :
_host(host),_uri(uri),_method(method),_user(user),_passwd(passwd)
{
_realm = "IP Camera(G9813)";
_nonce = "4f4746684f44686a4e4755364d6a526b4e475a6c5a57493d";
_nc = "00000001";
_qop = "auth";
_cnonce = "Yj2jyQll";
}
void QHttpAuth::printArgs()
{
qDebug() << "Host" << _host;
qDebug() << "Method" << _method;
qDebug() << "Url" << _uri;
qDebug() << "User" << _user;
qDebug() << "Passwd" << _passwd;
}
void QHttpAuth::parseResponse(const QString &_data)
{
if (_data.isEmpty()) return;
QString strAuth = _data;
QRegExp regExp("qop=\"(\\S+)\"");
if (-1 != regExp.indexIn(strAuth)) {
_qop = regExp.cap(1);
}
regExp = QRegExp("nonce=\"(\\S+)\"");
if (-1 != regExp.indexIn(strAuth)) {
_nonce = regExp.cap(1);
}
int index = strAuth.indexOf("realm=\"") + 7;
int count = strAuth.indexOf(", nonce") - index -1;
_realm = strAuth.mid(index, count);
}
void QHttpAuth::setPostData(const QString &_type, const QString &_data)
{
_contentType = _type;
_postData = _data;
}
QString QHttpAuth::host()
{
return _host;
}
QString QHttpAuth::method()
{
return _method;
}
QString QHttpAuth::contentType()
{
return _contentType;
}
QByteArray QHttpAuth::content()
{
return _postData.toUtf8();
}
QString QHttpAuth::toMd5(const QByteArray &_content) const
{
QByteArray md5 = QCryptographicHash::hash(_content, QCryptographicHash::Md5);
return md5.toHex();
}
QString QHttpAuth::getReponse() const
{
#define PRINT_TEST 0
// md5(<username>:<realm>:<password>) --- sha1
QString strA1 = _user + ":" + _realm + ":" + _passwd;
QString md5A1 = toMd5(strA1.toUtf8());
#if PRINT_TEST
qDebug() << "a1 -> str:" << strA1;
qDebug() << "a1 -> md5:" << md5A1;
#endif
// md5(<method>:<url>) -- sha2
QString strA2 = _method + ":" + _uri;
QString md5A2 = toMd5(strA2.toUtf8());
#if PRINT_TEST
qDebug() << "a2 -> str:" << strA2;
qDebug() << "a2 -> md5:" << md5A2;
#endif
// md5(<sha1>:<nonce>:<nc>:<cnonce>:<qop>:<sha2>)
QString strResponse = md5A1 + ":" + _nonce + ":" + _nc + ":" + _cnonce + ":" + _qop + ":" + md5A2;
QString md5Response = toMd5(strResponse.toUtf8());
#if PRINT_TEST
qDebug() << "response -> str:" << strResponse;
qDebug() << "response -> md5:" << md5Response;
#endif
return md5Response;
}
QByteArray QHttpAuth::getHttpRequest(bool isAuth) const
{
QString strRequest = QString("%1 %2 HTTP/1.1\r\n").arg(_method).arg(_uri);
strRequest += QString("Host: %1\r\n").arg(_host);
strRequest += QString("Connection: keep-alive\r\n");
strRequest += QString("User-Agent: \"xiaomu_niyh\"\r\n");
strRequest += QString("Accept: \"*/*\"\r\n");
strRequest += QString("Accept-Encoding: gzip, deflate, br\r\n");
strRequest += QString("Accept-Language: zh-CN,zh;q=0.8\r\n");
if (isAuth)
{
strRequest += QString("Authorization: Digest username=\"%1\", ").arg(_user);
strRequest += QString("realm=\"%1\", ").arg(_realm);
strRequest
没有合适的资源?快使用搜索试试~ 我知道了~
Qt 利用海康摄像头的ISAPI协议进行抓图等操作
共8个文件
cpp:3个
h:2个
jpeg:1个
需积分: 46 42 下载量 54 浏览量
2022-09-30
10:07:23
上传
评论 1
收藏 126KB RAR 举报
温馨提示
Qt 利用海康摄像头的ISAPI协议进行抓图等操作,详细过程参考博客:https://blog.csdn.net/nigoole/article/details/125632600#comments_23468337
资源详情
资源评论
资源推荐
收起资源包目录
QIsapiTest-src.rar (8个子文件)
QIsapiTest
QIsapiTest.pro 1KB
hot_pic_20220616175019.jpeg 456KB
qisapithread.h 2KB
qisapithread.cpp 10KB
main.cpp 591B
mainwindow.ui 10KB
app_bin
mainwindow.cpp 6KB
mainwindow.h 1KB
共 8 条
- 1
lynnhua_
- 粉丝: 128
- 资源: 22
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0