-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
91 lines (79 loc) · 3.19 KB
/
Program.cs
File metadata and controls
91 lines (79 loc) · 3.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
using System;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
/// <summary>
/// This program demonstrates how to make an HTTP API call to a PTZOptics camera
/// to recall a preset position using Digest Authentication.
/// It prompts the user for connection details and sends a GET request
/// to the camera's CGI endpoint to trigger the preset recall.
/// </summary>
class Program
{
static async Task Main(string[] args)
{
// Get connection details once
Console.Write("Enter IP Address (e.g., 192.168.1.50): ");
string ipAddress = Console.ReadLine()?.Trim() ?? string.Empty;
Console.Write("Enter Username: ");
string username = Console.ReadLine()?.Trim() ?? string.Empty;
Console.Write("Enter Password: ");
string password = Console.ReadLine()?.Trim() ?? string.Empty;
// Configure the Handler for Digest Authentication
var handler = new HttpClientHandler
{
Credentials = new NetworkCredential(username, password)
};
using (var client = new HttpClient(handler))
{
while (true)
{
Console.Write("\nEnter Preset Number (or 'exit' to quit): ");
string input = Console.ReadLine()?.Trim() ?? string.Empty;
if (input.ToLower() == "exit")
{
Console.WriteLine("Exiting...");
break;
}
if (string.IsNullOrWhiteSpace(input))
{
Console.WriteLine("Please enter a valid preset number.");
continue;
}
// Construct the URL
string url = $"http://{ipAddress}/cgi-bin/ptzctrl.cgi?ptzcmd&poscall&{input}";
Console.WriteLine($"\nTargeting: {url}");
Console.WriteLine("Sending request...");
try
{
// Send the GET request
HttpResponseMessage response = await client.GetAsync(url);
// Output Results
if (response.IsSuccessStatusCode)
{
Console.WriteLine("Success! Command sent.");
Console.WriteLine($"Status Code: {response.StatusCode}");
string content = await response.Content.ReadAsStringAsync();
if (!string.IsNullOrWhiteSpace(content))
{
Console.WriteLine($"Response: {content}");
}
}
else
{
Console.WriteLine($"Error: The camera returned status code {response.StatusCode}");
Console.WriteLine($"Reason: {response.ReasonPhrase}");
}
}
catch (HttpRequestException e)
{
Console.WriteLine($"\nNetwork Error: {e.Message}");
}
catch (Exception e)
{
Console.WriteLine($"\nAn unexpected error occurred: {e.Message}");
}
}
}
}
}