You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

95 lines
3.0 KiB

using EC.App.Entity;
using Flurl;
using Flurl.Http;
using Newtonsoft.Json.Linq;
using System;
using System.IO;
using System.Net;
using System.Net.Http;
namespace OnvifSocketServer
{
public class Program
{
public static void Main(string[] args)
{
Console.WriteLine("Hello World!");
TestOnvifUrl();
Console.ReadLine();
}
public static void TestOnvifUrl()
{
string onvifHttpUrl = AppConfig.GetOnvifHttpUrl();
string onvifHttpsUrl = AppConfig.GetOnvifHttpsUrl();
Uri baseUri = new Uri(onvifHttpUrl);
JObject result;
result = Request<JObject>(baseUri, "onvif/Curd/IsExist", HttpMethod.Get, new { ip = "192.168.1.65" });
result = Request<JObject>(baseUri, "onvif/Curd/Add", HttpMethod.Post,
new { ip = "192.168.1.65", username = "admin", password = "hk123456" });
result = Request<JObject>(baseUri, "onvif/Media/GetStreamUri", HttpMethod.Get, new { ip = "192.168.1.65" });
result = Request<JObject>(baseUri, "onvif/Media/GetSnapshotUri", HttpMethod.Get, new { ip = "192.168.1.65" });
DownloadSnapShot(result);
// 192.168.1.108
//result = Request<JObject>(baseUri, "onvif/Curd/Add", HttpMethod.Post,
// new { ip = "192.168.1.108", username = "admin", password = "hk123456" });
//result = Request<JObject>(baseUri, "onvif/Media/GetStreamUri", HttpMethod.Get, new { ip = "192.168.1.108" });
//result = Request<JObject>(baseUri, "onvif/Media/GetSnapshotUri", HttpMethod.Get, new { ip = "192.168.1.108" });
}
private static void DownloadSnapShot(JObject jObj)
{
RespParam<object> result = jObj.ToObject<RespParam<object>>();
string snapshotUrl = result.Data.ToString();
if (string.IsNullOrEmpty(snapshotUrl)) return;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(snapshotUrl);
request.Method = "GET";
request.PreAuthenticate = true;
request.Credentials = new NetworkCredential("admin", "hk123456");
using HttpWebResponse response = (HttpWebResponse)request.GetResponse();
using Stream stream = response.GetResponseStream();
using MemoryStream mStream = new();
byte[] buffer = new byte[1024];
int byteCount;
do
{
byteCount = stream.Read(buffer, 0, buffer.Length);
mStream.Write(buffer, 0, byteCount);
} while (byteCount > 0);
mStream.Position = 0;
string base64Str = Convert.ToBase64String(mStream.ToArray());
}
public static T Request<T>(Uri uri, string relativePath, HttpMethod httpMethod, object data)
{
Url url = uri.AbsoluteUri.AppendPathSegment(relativePath).SetQueryParams(data);
T result;
if (httpMethod == HttpMethod.Get)
{
result = url.GetAsync().ReceiveJson<T>().Result;
}
else if (httpMethod == HttpMethod.Post)
{
result = url.PostAsync().ReceiveJson<T>().Result;
}
else if (httpMethod == HttpMethod.Put)
{
result = url.PutAsync().ReceiveJson<T>().Result; ;
}
else if (httpMethod == HttpMethod.Delete)
{
result = url.DeleteAsync().ReceiveJson<T>().Result; ;
}
else
{
result = default;
}
Console.WriteLine(result);
return result;
}
}
}