C# 如何使用webrequest循环检查响应中的更改

C# 如何使用webrequest循环检查响应中的更改,c#,loops,response,webrequest,C#,Loops,Response,Webrequest,我今天要问你们如何做一个类似浏览器的请求,简而言之,我想对一个网站做一个web请求,让它保持活动状态,这样我就可以在一个循环中检查cookie和源代码中的更改了吗 这是我的实际代码: HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://localhost/test.php"); request.CookieContainer = new CookieContainer(); // n

我今天要问你们如何做一个类似浏览器的请求,简而言之,我想对一个网站做一个web请求,让它保持活动状态,这样我就可以在一个循环中检查cookie和源代码中的更改了吗

这是我的实际代码:

        HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://localhost/test.php");
        request.CookieContainer = new CookieContainer(); // needed to keep the session alive

        HttpWebResponse response = (HttpWebResponse)request.GetResponse();

        string value = null;

        if (response.StatusCode == HttpStatusCode.OK)
        {
            bool search = true;

            while(search == true)
            {
                for(int i = 0; i < response.Cookies.Count && search; i++)
                {
                    if (response.Cookies[i].Name == "test")
                    {
                        value = response.Cookies[i].Value;
                        search = false;
                    }
                }
            }
        }

        if (value != null)
            Console.WriteLine("found cookie : {0}", value);

        response.Close();
HttpWebRequest请求=(HttpWebRequest)WebRequest.Create(“http://localhost/test.php");
request.CookieContainer=新的CookieContainer();//需要使会话保持活动状态
HttpWebResponse=(HttpWebResponse)request.GetResponse();
字符串值=null;
if(response.StatusCode==HttpStatusCode.OK)
{
布尔搜索=真;
while(search==true)
{
对于(int i=0;i

提前谢谢你

只需将整个代码包装在一个循环中,然后删除已有的while循环。 如果您想通过获取整个流并将其或其散列存储在某个位置,然后将其与下一个响应进行比较来检查响应之间的差异。 还请记住放置一个try/catch,默认情况下只允许两个请求,除非您调整配置

bool search = true;

while (search == true) {
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://localhost/test.php");
    request.CookieContainer = new CookieContainer(); // needed to keep the session alive

    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
    string value = null;

    if (response.StatusCode == HttpStatusCode.OK)
    {           
        for (int i = 0; i < response.Cookies.Count && search; i++)
        {
            if (response.Cookies[i].Name == "test")
            {
                value = response.Cookies[i].Value;
                search = false;
            }
        }            
    }

    if (value != null)
    {
        Console.WriteLine("found cookie : {0}", value);
    }
    response.Close();
}
bool search=true;
while(search==true){
HttpWebRequest请求=(HttpWebRequest)WebRequest.Create(“http://localhost/test.php");
request.CookieContainer=new CookieContainer();//保持会话活动所需
HttpWebResponse=(HttpWebResponse)request.GetResponse();
字符串值=null;
if(response.StatusCode==HttpStatusCode.OK)
{           
对于(int i=0;i
谢谢您的回答,但问题是,我想要的cookie是在特定的时间后设置的,当然,每次新连接后都会重置,因此无法工作。请编辑您的问题以包含cookie要求?饼干是怎么做的?通过什么机制?我对HTTP协议的理解是,响应完成后,客户端或服务器关闭连接。为了完成类似的任务,您需要使用轮询类型的协议,如ws