博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Linux下onvif客户端获取ipc摄像头 GetProfiles:获取h265媒体信息文件
阅读量:5259 次
发布时间:2019-06-14

本文共 4123 字,大约阅读时间需要 13 分钟。

GetProfiles:获取媒体信息文件

鉴权:但是在使用这个接口之前是需要鉴权的。ONVIF协议规定,部分接口需要鉴权,部分接口不需要鉴权,在调用需要鉴权的接口时不使用鉴权,会导致接口调用失败。实现鉴权的方式之一可以调用gSOAP源码中的 soap_wsse_add_UsernameTokenDigest()函数。要安装依赖库OpenSSL

实现代码:

#include 
#include
#include
#include
#include "soapH.h"#include "stdsoap2.h"#include "soapStub.h"#include "wsseapi.h"#include "wsdd.nsmap" //命名空间static struct soap* ONVIF_Initsoap(struct SOAP_ENV__Header *header, const char *was_To, const char *was_Action, int timeout){ struct soap *soap = NULL; // soap环境变量 unsigned char macaddr[6]; char _HwId[1024]; unsigned int Flagrand; soap = soap_new(); if(soap == NULL) { printf("[%d]soap = NULL\n", __LINE__); return NULL; } soap_set_namespaces(soap, namespaces); // 设置soap的namespaces,即设置命名空间 // 设置超时(超过指定时间没有数据就退出) if(timeout > 0) { soap->recv_timeout = timeout; soap->send_timeout = timeout; soap->connect_timeout = timeout; } else { //Maximum waittime : 20s soap->recv_timeout = 20; soap->send_timeout = 20; soap->connect_timeout = 20; } soap_default_SOAP_ENV__Header(soap, header); //Create SessionID randomly,生成uuid(windows下叫guid,linux下叫uuid),格式为urn:uuid:8-4-4-4-12,由系统随机产生 srand((int)time(0)); Flagrand = rand()%9000 + 8888; macaddr[0] = 0x1; macaddr[1] = 0x2; macaddr[2] = 0x3; macaddr[3] = 0x4; macaddr[4] = 0x5; macaddr[5] = 0x6; sprintf(_HwId, "urn:uuid:%ud68a-1dd2-11b2-a105-%02X%02X%02X%02X%02X%02X", Flagrand, macaddr[0], macaddr[1], macaddr[2],macaddr[3],macaddr[4],macaddr[5]); header->wsa__MessageID = (char *)malloc(100); memset(header->wsa__MessageID, 0, 100); strncpy(header->wsa__MessageID, _HwId, strlen(_HwId)); //wsa__MessageID存放的是uuid if(was_Action != NULL) { header->wsa__Action = (char*)malloc(1024); memset(header->wsa__Action, '\0', 1024); strncpy(header->wsa__Action, was_Action, 1024); // } if(was_To != NULL) { header->wsa__To = (char *)malloc(1024); memset(header->wsa__To, '\0', 1024); strncpy(header->wsa__To, was_To, 1024);//"urn:schemas-xmlsoap-org:ws:2005:04:discovery"; } soap->header = header; return soap;}//释放函数void ONVIF_soap_delete(struct soap *soap){ soap_destroy(soap); // remove deserialized class instances (C++ only) soap_end(soap); // Clean up deserialized data (except class instances) and temporary data soap_free(soap); // Reset and deallocate the context created with soap_new or soap_copy}//鉴权static int ONVIF_SetAuthInfo(struct soap *soap, const char *username, const char *password){ int result = 0; if((NULL != username) || (NULL != password)){ soap_wsse_add_UsernameTokenDigest(soap, NULL, username, password); }else{ printf("un etAuth\n"); result = -1; } return result;}int main(int argc,char *argv[]){ int i = 0; int ret = 0; char media_addr[] = "http://172.168.0.211/onvif/media_service"; //GetCapabilities得到的地址 char media_addr2[] = "http://172.168.0.211/onvif/media2_service"; //GetServices得到的地址 struct SOAP_ENV__Header header; struct soap* soap = ONVIF_Initsoap(&header, NULL, NULL, 5); struct _tr2__GetProfiles tr2__GetProfiles; struct _tr2__GetProfilesResponse tr2__GetProfilesResponse; tr2__GetProfiles.__sizeType = 1; tr2__GetProfiles.Token = NULL; tr2__GetProfiles.Type = NULL; ONVIF_SetAuthInfo(soap,"admin","123456"); //鉴权 soap_call___tr2__GetProfiles(soap, media_addr2, NULL, &tr2__GetProfiles, &tr2__GetProfilesResponse); if(soap->error){ ret = -1; printf("soap error: %d, %s, %s\n", soap->error, *soap_faultcode(soap), *soap_faultstring(soap)); return ret; }else{ for(i=0; i

 

编译:gcc -o test get_ GetProfiles_test.c stdsoap2.c soapC.c md5.c dom.c mecevp.c smdevp.c threads.c wsaapi.c wsseapi.c soapClient.c -I import/ -DWITH_OPENSSL -lssl -lcrypto -ldl -pthread 

结果:

 

转载于:https://www.cnblogs.com/croxd/p/10683598.html

你可能感兴趣的文章
IOS解析XML
查看>>
Python3多线程爬取meizitu的图片
查看>>
树状数组及其他特别简单的扩展
查看>>
zookeeper适用场景:分布式锁实现
查看>>
110104_LC-Display(液晶显示屏)
查看>>
httpd_Vhosts文件的配置
查看>>
php学习笔记
查看>>
普通求素数和线性筛素数
查看>>
PHP截取中英文混合字符
查看>>
【洛谷P1816 忠诚】线段树
查看>>
电子眼抓拍大解密
查看>>
poj 1331 Multiply
查看>>
tomcat7的数据库连接池tomcatjdbc的25个优势
查看>>
Html 小插件5 百度搜索代码2
查看>>
P1107 最大整数
查看>>
多进程与多线程的区别
查看>>
Ubuntu(虚拟机)下安装Qt5.5.1
查看>>
java.io.IOException: read failed, socket might closed or timeout, read ret: -1
查看>>
java 常用命令
查看>>
CodeForces Round #545 Div.2
查看>>