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(baseUri, "onvif/Curd/IsExist", HttpMethod.Get, new { ip = "192.168.1.65" }); result = Request(baseUri, "onvif/Curd/Add", HttpMethod.Post, new { ip = "192.168.1.65", username = "admin", password = "hk123456" }); result = Request(baseUri, "onvif/Media/GetStreamUri", HttpMethod.Get, new { ip = "192.168.1.65" }); result = Request(baseUri, "onvif/Media/GetSnapshotUri", HttpMethod.Get, new { ip = "192.168.1.65" }); DownloadSnapShot(result); // 192.168.1.108 //result = Request(baseUri, "onvif/Curd/Add", HttpMethod.Post, // new { ip = "192.168.1.108", username = "admin", password = "hk123456" }); //result = Request(baseUri, "onvif/Media/GetStreamUri", HttpMethod.Get, new { ip = "192.168.1.108" }); //result = Request(baseUri, "onvif/Media/GetSnapshotUri", HttpMethod.Get, new { ip = "192.168.1.108" }); } private static void DownloadSnapShot(JObject jObj) { RespParam result = jObj.ToObject>(); 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(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().Result; } else if (httpMethod == HttpMethod.Post) { result = url.PostAsync().ReceiveJson().Result; } else if (httpMethod == HttpMethod.Put) { result = url.PutAsync().ReceiveJson().Result; ; } else if (httpMethod == HttpMethod.Delete) { result = url.DeleteAsync().ReceiveJson().Result; ; } else { result = default; } Console.WriteLine(result); return result; } } }