|
@@ -1244,46 +1244,70 @@ namespace PDF_Master.Helper
|
|
|
|
|
|
}
|
|
|
//监听订阅行为
|
|
|
- public static void ListenerSubscription()
|
|
|
+ public static async Task ListenerSubscriptionAsync()
|
|
|
{
|
|
|
string url = "http://127.0.0.1:5544/"; // 监听的URL地址
|
|
|
|
|
|
using (HttpListener listener = new HttpListener())
|
|
|
{
|
|
|
- listener.Prefixes.Add(url); // 添加要监听的前缀
|
|
|
|
|
|
+ listener.Prefixes.Add(url); // 添加要监听的前缀
|
|
|
listener.Start(); // 启动监听
|
|
|
-
|
|
|
Console.WriteLine("Listening for GET requests on {0}", url);
|
|
|
-
|
|
|
+ bool isProcessingRequest = false;
|
|
|
while (true)
|
|
|
{
|
|
|
- HttpListenerContext context = listener.GetContext();
|
|
|
-
|
|
|
- // 处理GET请求
|
|
|
- if (context.Request.HttpMethod == "GET")
|
|
|
+ try
|
|
|
{
|
|
|
- // 获取请求的路径和查询参数
|
|
|
- string path = context.Request.Url.AbsolutePath;
|
|
|
- string query = context.Request.Url.Query;
|
|
|
+ HttpListenerContext context = await listener.GetContextAsync();
|
|
|
|
|
|
- // 构建响应内容
|
|
|
- string responseText = $"Received GET request on {path}{query}";
|
|
|
+ // 如果已经在处理请求,则忽略新的请求
|
|
|
+ if (isProcessingRequest)
|
|
|
+ {
|
|
|
+ context.Response.StatusCode = (int)HttpStatusCode.ServiceUnavailable;
|
|
|
+ context.Response.Close();
|
|
|
+ continue;
|
|
|
+ }
|
|
|
|
|
|
- byte[] responseBytes = System.Text.Encoding.UTF8.GetBytes(responseText);
|
|
|
- context.Response.ContentLength64 = responseBytes.Length;
|
|
|
+ // 设置标志变量为 true,表示正在处理请求
|
|
|
+ isProcessingRequest = true;
|
|
|
|
|
|
- // 发送响应
|
|
|
- context.Response.OutputStream.Write(responseBytes, 0, responseBytes.Length);
|
|
|
- }
|
|
|
+ // 处理GET请求
|
|
|
+ if (context.Request.HttpMethod == "GET")
|
|
|
+ {
|
|
|
+ // 获取请求的路径和查询参数
|
|
|
+ string path = context.Request.Url.AbsolutePath;
|
|
|
+ string query = context.Request.Url.Query;
|
|
|
+
|
|
|
+ // 构建响应内容
|
|
|
+ string responseText = $"Received GET request on {path}{query}";
|
|
|
|
|
|
- // 关闭响应
|
|
|
- context.Response.Close();
|
|
|
+ byte[] responseBytes = System.Text.Encoding.UTF8.GetBytes(responseText);
|
|
|
+ context.Response.ContentLength64 = responseBytes.Length;
|
|
|
+
|
|
|
+ // 发送响应
|
|
|
+ await context.Response.OutputStream.WriteAsync(responseBytes, 0, responseBytes.Length);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 关闭响应
|
|
|
+ context.Response.Close();
|
|
|
+
|
|
|
+ // 将标志变量重置为 false,表示已完成请求处理
|
|
|
+ isProcessingRequest = false;
|
|
|
+ }
|
|
|
+ catch (Exception ex)
|
|
|
+ {
|
|
|
+ // 处理异常
|
|
|
+ Console.WriteLine("Error occurred: {0}", ex.Message);
|
|
|
+ // 在此处可以添加适当的异常处理逻辑
|
|
|
+ }
|
|
|
}
|
|
|
+
|
|
|
}
|
|
|
}
|
|
|
+ }
|
|
|
|
|
|
|
|
|
- }
|
|
|
+
|
|
|
}
|
|
|
|