简体   繁体   English

HttpClient处理服务器注销

[英]Httpclient handling server logouts

Class Site define a website that has methods to login and parse data. 网站类定义了一个网站,该网站具有登录和解析数据的方法。 I have a separate class Browser that contains an httpclient class that is used extensively by all instances of Site. 我有一个单独的类浏览器,其中包含一个httpclient类,该类被Site的所有实例广泛使用。

What I would like to do is implement exception handling into Browser so that if Site is ever logged out (this would be determined by examining the response), it would attempt to log back in, and retry the same get request as if nothing ever happened. 我想做的是在浏览器中实现异常处理,以便如果站点已注销(这将通过检查响应来确定),它将尝试重新登录,然后重试相同的get请求,就好像什么都没有发生。 However, I am not sure how to structure this since the login method would be located in the Site object. 但是,由于登录方法位于Site对象中,因此我不确定如何构造它。 Pseudo-code would be something like this 伪代码将是这样的

static class Browser
    static get_url(string url)
    try
    { 
        fetch url with http client; 
        if (session logged out) 
            throw logged out exception
    }
    catch (logged out exception)
    { 
        site.login, get_url(url) 
    }

class Site
    update(url)
    {
        content = Browser.get_url(url)
    }

What i would do is add some sort of ValidateLogin method before each execution of a GetUrl method. 我要做的是在每次执行GetUrl方法之前添加某种ValidateLogin方法。 This can be a cached value which expires every X minutes, or can be a simply REST request send through the browser. 这可以是每X分钟过期的缓存值,也可以是通过浏览器发送的简单REST请求。

It would look along the lines of this: 它看起来像这样:

public class Site
{  
   public void Update(string url)
   {
       var isLoggedIn = Browser.ValidateLogin();
       if (!isLoggedIn)
       {
          this.Login();
       }

       Browser.GetUrl(url);
   }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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