문제

첫 번째 요청에 로그인하는 CookieContainer를 재사용하는 다음 코드가 있지만 쿠키 컨테이너를 사용하여 요청에 따라 사용합니다.

일정 기간이 지나면 사이트가 세션 타임 아웃을 제공하면 로그인을 다시 수행해야합니다.

Q : 타임 아웃이 발생했는지 또는 쿠키 컨테이너 개체와 함께 (쿠키 컨테이너 개체)를 결정할 수 있습니까? 또는 '세션 타임 아웃'과 같은 텍스트가 포함 된 httpwebrepresponse에서 발생했는지 확인하는 것이 가장 좋습니다. 이것을하는 가장 좋은 방법은 무엇입니까?

        private static CookieContainer _cookieContainer;
    private static CookieContainer CurrentCookieContainer
    {
        get
        {
            if (_cookieContainer == null || _cookieContainer.Count == 0)
            {
                lock (_lock)
                {
                    if (_cookieContainer == null || _cookieContainer.Count == 0)
                    {
                        //_cookieContainer.GetCookies(
                        _cookieContainer = DoLogin();
                    }
                }
            }

            return _cookieContainer;
        }
        set
        {
            _cookieContainer = value;
        }
    }

그런 다음이 방법은 컨테이너를 호출합니다.

        public static string SomeMethod(SomeParams p)
        {
            HttpWebRequest request_thirdPartyEnquiryDetails = (HttpWebRequest)WebRequest.Create(thirdPartyEnquiryDetails);
            CookieContainer cookieContainer = CurrentCookieContainer;
            request_thirdPartyEnquiryDetails.CookieContainer = cookieContainer;
//... and it goes on to submit a search and return the response
        }
도움이 되었습니까?

해결책

글쎄, 타임 아웃은 30 분이기 때문에 25 분 후에 로그인을 반복하도록 설정했습니다.

private static DateTime? lastLoggedIn;

    private static CookieContainer _cookieContainer;
    private static CookieContainer CurrentCookieContainer
    {
        get
        {
            if (_cookieContainer == null || _cookieContainer.Count == 0 || !lastLoggedIn.HasValue || lastLoggedIn.Value.AddMinutes(25) < DateTime.Now)
            {
                lock (_lock)
                {
                    if (_cookieContainer == null || _cookieContainer.Count == 0 || !lastLoggedIn.HasValue || lastLoggedIn.Value.AddMinutes(25) < DateTime.Now)
                    {
                        _cookieContainer = DoLogin();
                        lastLoggedIn = DateTime.Now;
                    }                        
                }
            }

            return _cookieContainer;
        }
        set
        {
            _cookieContainer = value;
        }
    }

추가 예방 조치로, 나는 페이지 세션이 시간이 끝날 때 반환되는 텍스트에 대한 httpresponse를 확인합니다 (지금은 보이지 않지만). 이런 일이 발생하면 LastLoggedin 날짜를 NULL로 설정하고 검색 메소드를 다시 실행합니다.

다른 팁

CookieContainer.getCookies (String URI) 메소드를 사용하여 도메인의 모든 쿠키를 추출 할 수 있습니다. 이 CookieCollection을 사용하면 관심있는 쿠키를 얻고 만료 된 속성을 확인하여 만료되었는지 확인할 수 있습니다.

주목해야 할 사항이 있습니다. 쿠키가 유효하더라도 세션이 종료 될 수 있습니다. IIS는 웹 응용 프로그램이 실행되는 앱 도메인을 다시 시작할 수 있으며이 경우 모든 인증 된 사용자가 세션 데이터를 잃을 수 있습니다. 따라서 쿠키를 점검하는 것만으로는 로그인을 유지하기에 충분하지 않습니다.

무엇을 달성하고 싶은지 잘 모르겠지만 CookieContainer는 .add (쿠키) 및 .getCookies (URI) 메소드에 버그가 있음을 알 수 있습니다.

세부 사항을보고 여기에서 수정하십시오.

http://dot-net-expertise.blogspot.com/2009/10/cookiecontainer-domain handling-bug-fix.html

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top