|
|
@ -1,7 +1,10 @@ |
|
|
|
using Flurl; |
|
|
|
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 |
|
|
@ -21,58 +24,69 @@ namespace OnvifSocketServer |
|
|
|
string onvifHttpsUrl = AppConfig.GetOnvifHttpsUrl(); |
|
|
|
Uri baseUri = new Uri(onvifHttpUrl); |
|
|
|
JObject result; |
|
|
|
Console.WriteLine(onvifHttpUrl); |
|
|
|
|
|
|
|
// Get
|
|
|
|
result = baseUri.AbsoluteUri |
|
|
|
.AppendPathSegment("onvif/Curd/IsExist") |
|
|
|
.SetQueryParams(new { ip = "192.168.1.108" }) |
|
|
|
.GetAsync() |
|
|
|
.ReceiveJson<JObject>().Result; |
|
|
|
Console.WriteLine(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); |
|
|
|
|
|
|
|
// Post
|
|
|
|
result = baseUri.AbsoluteUri |
|
|
|
.AppendPathSegment("onvif/Curd/Add") |
|
|
|
.SetQueryParams(new { ip = "192.168.1.65", username = "admin", password = "hk123456" }) |
|
|
|
.PostAsync() |
|
|
|
.ReceiveJson<JObject>().Result; |
|
|
|
Console.WriteLine(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" });
|
|
|
|
} |
|
|
|
|
|
|
|
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" }); |
|
|
|
private static void DownloadSnapShot(JObject jObj) |
|
|
|
{ |
|
|
|
RespParam<object> result = jObj.ToObject<RespParam<object>>(); |
|
|
|
string snapshotUrl = result.Data.ToString(); |
|
|
|
if (string.IsNullOrEmpty(snapshotUrl)) return; |
|
|
|
|
|
|
|
// Post
|
|
|
|
result = Request(baseUri, "onvif/Curd/Add", HttpMethod.Post, |
|
|
|
new { ip = "192.168.1.108", username = "admin", password = "hk123456" }); |
|
|
|
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(snapshotUrl); |
|
|
|
request.Method = "GET"; |
|
|
|
request.PreAuthenticate = true; |
|
|
|
request.Credentials = new NetworkCredential("admin", "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" }); |
|
|
|
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 JObject Request(Uri uri, string relativePath, HttpMethod httpMethod, object data) |
|
|
|
public static T Request<T>(Uri uri, string relativePath, HttpMethod httpMethod, object data) |
|
|
|
{ |
|
|
|
Url url = uri.AbsoluteUri.AppendPathSegment(relativePath).SetQueryParams(data); |
|
|
|
JObject result; |
|
|
|
T result; |
|
|
|
if (httpMethod == HttpMethod.Get) |
|
|
|
{ |
|
|
|
result = url.GetAsync().ReceiveJson<JObject>().Result; |
|
|
|
result = url.GetAsync().ReceiveJson<T>().Result; |
|
|
|
} |
|
|
|
else if (httpMethod == HttpMethod.Post) |
|
|
|
{ |
|
|
|
result = url.PostAsync().ReceiveJson<JObject>().Result; |
|
|
|
result = url.PostAsync().ReceiveJson<T>().Result; |
|
|
|
} |
|
|
|
else if (httpMethod == HttpMethod.Put) |
|
|
|
{ |
|
|
|
result = url.PutAsync().ReceiveJson<JObject>().Result; ; |
|
|
|
result = url.PutAsync().ReceiveJson<T>().Result; ; |
|
|
|
} |
|
|
|
else if (httpMethod == HttpMethod.Delete) |
|
|
|
{ |
|
|
|
result = url.DeleteAsync().ReceiveJson<JObject>().Result; ; |
|
|
|
result = url.DeleteAsync().ReceiveJson<T>().Result; ; |
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
|
result = null; |
|
|
|
result = default; |
|
|
|
} |
|
|
|
Console.WriteLine(result); |
|
|
|
return result; |
|
|
|