Example #1
0
        public void GetSid()
        {
            try
            {
                ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | (SecurityProtocolType)768 | (SecurityProtocolType)3072;
            }
            catch
            {
                ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls;
            }

            HttpWebRequest APIRequest = (HttpWebRequest)WebRequest.Create(ApiServerUri[1] + "/v1/auth/");

            try
            {
                APIRequest.Proxy = (Proxy != "") ? (new WebProxy(Proxy)) : (null);
            }
            catch (UriFormatException)
            {
                APIRequest.Proxy = null;
            }
            APIRequest.Method    = "POST";
            APIRequest.UserAgent = this.SidUA;
            var res = X2chUA.Split(':');

            APIRequest.Headers.Add("X-2ch-UA", (res.Length == 1) ? (X2chUA) : (res[1]));
            APIRequest.Timeout     = 40000;
            APIRequest.ContentType = "application/x-www-form-urlencoded";
            APIRequest.ServicePoint.Expect100Continue = false;

            String Value = "ID=" + HttpUtility.UrlEncode(RouninID);

            Value += "&PW=" + HttpUtility.UrlEncode(RouninPW);
            Value += "&KY=" + HttpUtility.UrlEncode(AppKey);
            byte[] postData = Encoding.ASCII.GetBytes(Value);

            APIRequest.ContentLength = postData.Length;
            try
            {
                using (Stream PostStream = APIRequest.GetRequestStream())
                {
                    PostStream.Write(postData, 0, postData.Length);
                    WebResponse wres = APIRequest.GetResponse();
                    using (StreamReader Res = new StreamReader(wres.GetResponseStream()))
                    {
                        String ResData = Res.ReadToEnd();
                        //
                        m_SID = ResData.Split(':')[1];
                        if (ResData.IndexOf("ERROR:") > -1 || m_SID == "")
                        {
                            ViewModel.OnModelNotice("SessionIDの取得に失敗しました。\n" + ResData, true);
                            m_SID        = m_DefaultSID;
                            GetSIDFailed = true;
                        }
                    }
                    ViewModel.RestartStatus = 13;
                    wres?.Close();
                }
            }
            catch (WebException err)
            {
                if (m_SID == "")
                {
                    m_SID = m_DefaultSID;
                }
                if (ViewModel.RestartStatus > 0)
                {
                    ++ViewModel.RestartStatus;
                }
                ViewModel.OnModelNotice("SessionIDの取得に失敗しました。\n" + err.ToString());
                err.Response?.Close();
                GetSIDFailed = true;
                return;
            }

            foreach (KeyValuePair <string, HoboData> key in m_HoboCache.AsParallel())
            {
                String hobo = KeyGen("/v1/" + key.Key + m_SID + AppKey);
                m_HoboCache[key.Key].hobo = Encoding.ASCII.GetBytes("sid=" + m_SID + "&hobo=" + hobo + "&appkey=" + AppKey);
            }
            m_HoboCheck.Change(3600000 * 8, Timeout.Infinite);
            sidup = 0;
        }