服务器是接收通知的代码 可以写一份C# 版本么
时间: 2023-06-25 12:07:02 浏览: 56
当然可以,以下是C#版本的接收通知代码示例:
```csharp
using System;
using System.IO;
using System.Net;
using System.Text;
class Program {
static void Main(string[] args) {
HttpListener listener = new HttpListener();
listener.Prefixes.Add("http://localhost:8000/"); // 设置监听端口和地址
listener.Start(); // 开始监听
Console.WriteLine("Listening...");
while (true) {
HttpListenerContext context = listener.GetContext(); // 获取请求上下文
HttpListenerRequest request = context.Request;
HttpListenerResponse response = context.Response;
if (request.HttpMethod == "POST") { // 判断请求的方法是否为POST
Console.WriteLine("Request received.");
Stream body = request.InputStream;
Encoding encoding = request.ContentEncoding;
StreamReader reader = new StreamReader(body, encoding);
string content = reader.ReadToEnd();
Console.WriteLine("Content: " + content); // 打印接收到的内容
byte[] buffer = Encoding.UTF8.GetBytes("Received"); // 返回响应
response.ContentLength64 = buffer.Length;
response.OutputStream.Write(buffer, 0, buffer.Length);
} else {
response.StatusCode = 405; // 不支持其他HTTP方法
}
response.Close();
}
}
}
```
以上代码创建了一个HTTP监听器,在本地8000端口上监听POST请求,并打印接收到的内容。你可以根据实际需要修改监听地址和端口,以及处理接收到的通知的逻辑。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)