9

In my project I'm using C# app client and tomcat6 web application server. I wrote this snippet in the C# client:

public bool isServerOnline()
{
        Boolean ret = false;

        try
        {
            HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(VPMacro.MacroUploader.SERVER_URL);
            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
            Log.e("InternetUtils - isServerOnline - " + we.Status);
            return false;
        }
}

Everytime I invoke this method, I get a new session at server side. I suppose it's because I should use HTTP cookies in my client. But I don't know how to do that, can you help me?

jgauffin
  • 99,844
  • 45
  • 235
  • 372
CeccoCQ
  • 3,746
  • 12
  • 50
  • 81

1 Answers1

27

You must use a CookieContainer and keep the instance between calls.

private CookieContainer cookieContainer = new CookieContainer();
public bool isServerOnline()
{
        Boolean ret = false;

        try
        {
            HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(VPMacro.MacroUploader.SERVER_URL);
            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
            Log.e("InternetUtils - isServerOnline - " + we.Status);
            return false;
        }
}
Guillaume
  • 12,824
  • 3
  • 40
  • 48
  • 1
    Thanks, I try it and let you know if this works. But I've a question: if cookie expired how can I manage this? – CeccoCQ Jun 08 '11 at 07:50
  • 1
    Another question, but I shouldn't get the Cookie from the response? – CeccoCQ Jun 08 '11 at 07:54
  • 1
    The container will be filled, you don't have to care about response cookie. Also the size of the container is limited and when the limit is reached, expired cookies are removed. – Guillaume Jun 08 '11 at 08:53
  • 1
    ASP. Net mvc is a server side framework. The cookie container is client side. So yes it will work regardless of server side stack as long as cookies are used – Guillaume Sep 29 '15 at 16:43
  • can i use this method to preserve basic authentication session? Also can i use same cookie container instance for several different username and password pairs? – Tornike Shavishvili Jan 23 '20 at 07:07
  • @TornikeShavishvili basic authentication relies on Authorization HTTP header, not cookies. You'll have to keep sending this header on every request. Feel free to post a question if you need more help. – Guillaume Jan 27 '20 at 08:14