通知的保持活动连接C#

通知的保持活动连接C#,c#,windows,post,notifications,keep-alive,C#,Windows,Post,Notifications,Keep Alive,每当从网站(需要POST方法登录)获取的文本发生更改时,我需要在用户的桌面上显示通知。该网站有一个包含文本的表格,管理员可以编辑该表格,因此包含HTML页面源代码的字符串将发生更改。我认为连接应该始终处于活动状态,以便提供实时通知。我已经有代码执行登录到网站,但连接关闭时,该方法结束 我的问题是:归档保持活动连接并在桌面上显示通知的最佳方式是什么 以下是登录代码: public async static Task<string> LoginAsync(string reqUri, s

每当从网站(需要POST方法登录)获取的文本发生更改时,我需要在用户的桌面上显示通知。该网站有一个包含文本的表格,管理员可以编辑该表格,因此包含HTML页面源代码的字符串将发生更改。我认为连接应该始终处于活动状态,以便提供实时通知。我已经有代码执行登录到网站,但连接关闭时,该方法结束

我的问题是:归档保持活动连接并在桌面上显示通知的最佳方式是什么

以下是登录代码:

public async static Task<string> LoginAsync(string reqUri, string postData)
    {
        // Create a request using a URL that can receive a post. 
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(reqUri);

        // Set the Method property of the request to POST.
        request.Method = "POST";

        // Set the ContentType property of the WebRequest.
        request.ContentType = "application/x-www-form-urlencoded";

        // Convert POST data to a byte array.
        byte[] data = Encoding.UTF8.GetBytes(postData);

        // Get request stream and write data to it
        using (var requestStream = await Task<Stream>.Factory.FromAsync(request.BeginGetRequestStream, request.EndGetRequestStream, null))
        {
            await requestStream.WriteAsync(data, 0, data.Length);
        }

        return await GetResponse(request);
    }


    private async static Task<string> GetResponse(HttpWebRequest request)
    {
        string _response = null;

        // Get response from the website
        using (var response = (HttpWebResponse)(await Task<WebResponse>.Factory.FromAsync(request.BeginGetResponse, request.EndGetResponse, null)))
        {
            // Get the response stream
            using (Stream responseStream = response.GetResponseStream())
            {
                using (StreamReader sr = new StreamReader(responseStream, Encoding.GetEncoding("iso-8859-1")))
                {
                    // Get the response string from the website
                    _response = await sr.ReadToEndAsync();
                }
            }
        }

        return _response;
    }
public异步静态任务LoginAsync(string reqUri,string postData)
{
//使用可以接收帖子的URL创建请求。
HttpWebRequest请求=(HttpWebRequest)WebRequest.Create(reqUri);
//将请求的Method属性设置为POST。
request.Method=“POST”;
//设置WebRequest的ContentType属性。
request.ContentType=“application/x-www-form-urlencoded”;
//将POST数据转换为字节数组。
byte[]data=Encoding.UTF8.GetBytes(postData);
//获取请求流并将数据写入其中
使用(var requestStream=await Task.Factory.fromsync(request.BeginGetRequestStream,request.EndGetRequestStream,null))
{
wait requestStream.WriteAsync(数据,0,数据.Length);
}
返回等待GetResponse(请求);
}
专用异步静态任务GetResponse(HttpWebRequest请求)
{
字符串_response=null;
//从网站上获得回复
使用(var response=(HttpWebResponse)(wait Task.Factory.fromsync(request.BeginGetResponse,request.EndGetResponse,null)))
{
//获取响应流
使用(Stream responseStream=response.GetResponseStream())
{
使用(StreamReader sr=新的StreamReader(responseStream,Encoding.GetEncoding(“iso-8859-1”))
{
//从网站获取响应字符串
_response=wait sr.ReadToEndAsync();
}
}
}
返回响应;
}

最好的方法是使用信号器,而不是HTTP。如果您不控制服务器,且SignalR不可用,则您需要在某个时间间隔内进行轮询。

我不控制服务器,因此无法卸载任何ASP.NET页面以使用SignalIR。您是否建议我在桌面程序上运行计时器,并在每次滴答事件发生时执行登录以检索数据?如果是这样的话,这种方法会不会使服务器过载(因为我有大约1000个到单个服务器的连接,我需要将间隔至少设置为3秒以获得快速通知)?如果您根本不控制服务器,并且只有HTTP可用,那么轮询是唯一的选项。如果您担心负载,您可以设置一个中间服务器进行轮询,并使用Signal更新其客户端(桌面应用程序)。