Repository navigation
Expand file tree
/
Copy pathNavHttpClient.cs
More file actions
129 lines (116 loc) · 5.15 KB
/
Copy pathNavHttpClient.cs
File metadata and controls
129 lines (116 loc) · 5.15 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
using System;
using System.Net.Http;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
namespace NavHttpClientAsync
{
[Guid("2fb45a77-9e85-431d-9ac9-d030107deebd")]
[ComVisible(true)]
public interface INavHttpClient
{
string HelloWorld();
void SendHttpRequest(string url, string method, string headers, string body, string filePath);
}
[Guid("79b05537-33c7-4f24-be8b-581373154e71")]
[ClassInterface(ClassInterfaceType.None)]
[ComVisible(true)]
[ProgId("NavHttpClientAsync.NavHttpClient")]
public class NavHttpClient : INavHttpClient
{
private static readonly HttpClient httpClient = new HttpClient();
public string HelloWorld()
{
return "Hello World!";
}
public void SendHttpRequest(string url, string method, string headers, string body, string filePath)
{
// Fire and forget - returns immediately without blocking NAV
Task.Run(async () =>
{
try
{
await ExecuteHttpRequestAsync(url, method, headers, body, filePath);
}
catch (Exception ex)
{
// Log error to file if path provided
if (!string.IsNullOrEmpty(filePath))
{
LogToFile(filePath, $"[{DateTime.Now:yyyy-MM-dd HH:mm:ss}] ERROR: {ex.Message}\r\n");
}
System.Diagnostics.Debug.WriteLine($"HTTP Request Error: {ex.Message}");
}
});
}
private async Task ExecuteHttpRequestAsync(string url, string method, string headers, string body, string filePath)
{
using (var request = new HttpRequestMessage())
{
// Set HTTP method
request.Method = new HttpMethod(method.ToUpper());
request.RequestUri = new Uri(url);
// Parse and add headers (format: "Header1:Value1|Header2:Value2")
if (!string.IsNullOrEmpty(headers))
{
var headerPairs = headers.Split('|');
foreach (var headerPair in headerPairs)
{
var parts = headerPair.Split(new[] { ':' }, 2);
if (parts.Length == 2)
{
var headerName = parts[0].Trim();
var headerValue = parts[1].Trim();
// Try to add to request headers first
if (!request.Headers.TryAddWithoutValidation(headerName, headerValue))
{
// If it fails, it might be a content header
if (request.Content == null)
{
request.Content = new StringContent(string.Empty);
}
request.Content.Headers.TryAddWithoutValidation(headerName, headerValue);
}
}
}
}
// Add body for POST, PUT, PATCH
if (!string.IsNullOrEmpty(body) && (method.Equals("POST", StringComparison.OrdinalIgnoreCase) ||
method.Equals("PUT", StringComparison.OrdinalIgnoreCase) ||
method.Equals("PATCH", StringComparison.OrdinalIgnoreCase)))
{
request.Content = new StringContent(body, Encoding.UTF8, "application/json");
}
// Send the request
var response = await httpClient.SendAsync(request);
// Read response
var responseContent = await response.Content.ReadAsStringAsync();
System.Diagnostics.Debug.WriteLine($"Response Status: {response.StatusCode}");
System.Diagnostics.Debug.WriteLine($"Response Body: {responseContent}");
// Log to file if path provided
if (!string.IsNullOrEmpty(filePath))
{
var logEntry = new StringBuilder();
logEntry.AppendLine($"[{DateTime.Now:yyyy-MM-dd HH:mm:ss}]");
logEntry.AppendLine($"URL: {url}");
logEntry.AppendLine($"Method: {method}");
logEntry.AppendLine($"Status: {(int)response.StatusCode} {response.StatusCode}");
logEntry.AppendLine($"Response: {responseContent}");
logEntry.AppendLine(new string('-', 80));
LogToFile(filePath, logEntry.ToString());
}
}
}
private void LogToFile(string filePath, string content)
{
try
{
System.IO.File.AppendAllText(filePath, content);
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine($"Failed to write to log file: {ex.Message}");
}
}
}
}