代码之家  ›  专栏  ›  技术社区  ›  santosh singh

当客户端是Windows/Console应用程序时,WebService如何维护会话?

  •  3
  • santosh singh  · 技术社区  · 14 年前

    当客户端是Windows/Console应用程序时,WebService如何维护会话?

    3 回复  |  直到 10 年前
        1
  •  2
  •   SLaks    14 年前

    使用Cookie。

    CookieContainer . (假设您正在使用 HttpWebRequest )

        2
  •  2
  •   davisoa    14 年前

    在封面下,C WebClient正在存储Web服务提供给它的cookie。

        3
  •  0
  •   Devraj Gadhavi    10 年前

    如果有人感兴趣,这里有一些示例代码。

    class Program
    {
        static void Main(string[] args)
        {
            CookieContainer session = new CookieContainer();
    
            HttpWebRequest httpSomeRequest = (HttpWebRequest)WebRequest.Create("http://localhost:8080/someURL");
            httpSomeRequest.CookieContainer = session;
            httpSomeRequest.GetResponse();
    
            HttpWebRequest httpSomeOtherRequest  = (HttpWebRequest)WebRequest.Create("http://localhost:8080/someOtherURL");
            httpSomeOtherRequest.CookieContainer = session;
            httpSomeOtherRequest.GetResponse();
        }
    }
    

    我们只需要确保 HttpWebRequest 制造,使用相同的 CookieContainer 实例。