简体   繁体   中英

How do I stay on the page using HttpWebRequest?

So I am trying to simulate a person being on my website, in this case it's my console application.

I can connect to it using a HttpWebRequest and create a WebRequest but it doesn't show as a person being on the website in my dashboard. However when I manually get on my website through my web browser it says that someone is online on the website in my dashboard system (WordPress),

So my question is, how do I accomplish the same thing, would I have to create a Socket connection? Or is this possible by using KeepAlive because I think the issue is that it's not on the page for long enough, it connect and gets the request but it doesnt actually establish a connection if that makes any sense. That's just my theory please correct me if I am wrong.

public static bool isServerOnline()
        {
            Boolean ret = false;

            try
            {
                HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create("https://arcticinnovative.com");
                req.CookieContainer = cookieContainer; // <= HERE
                req.Method = "HEAD";
                req.KeepAlive = false;
                HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
                if (resp.StatusCode == HttpStatusCode.OK)
                {
                    // HTTP = 200 - Internet connection available, server online
                    ret = true;
                }
                resp.Close();
                return ret;

            }
            catch (WebException we)
            {
                // Exception - connection not available
                Debug.Print("InternetUtils - isServerOnline - " + we.Status);
                return false;
            }
        }

根据此处的文档,您可以将KeepAlive设置为true以保持持久连接。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM