public static AndroidClientHandler CreateAndroidClientHandler() { // https://docs.microsoft.com/zh-cn/xamarin/android/app-fundamentals/http-stack?context=xamarin%2Fcross-platform&tabs=macos var handler = new AndroidClientHandler(); AddFiddlerRootCertificate(handler); GeneralHttpClientFactory.SetProxyToHandler(GeneralHttpClientFactory.DefaultProxy, handler); return(handler); }
/// <summary> /// Synchronise this authenticator's time with Google. We update our data record with the difference from our UTC time. /// </summary> public override void Sync() { // check if data is protected if (SecretKey == null && EncryptedData != null) { throw new WinAuthEncryptedSecretDataException(); } // don't retry for 5 minutes if (_lastSyncError >= DateTime.Now.AddMinutes(0 - SYNC_ERROR_MINUTES)) { return; } try { // we use the Header response field from a request to www.google.come HttpWebRequest request = GeneralHttpClientFactory.Create(TIME_SYNC_URL); request.Method = "GET"; request.ContentType = "text/html"; request.Timeout = 5000; // get response using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) { // OK? if (response.StatusCode != HttpStatusCode.OK) { throw new ApplicationException(string.Format("{0}: {1}", (int)response.StatusCode, response.StatusDescription)); } string headerdate = response.Headers["Date"]; if (string.IsNullOrEmpty(headerdate) == false) { DateTime dt; if (DateTime.TryParse(headerdate, out dt) == true) { // get as ms since epoch long dtms = Convert.ToInt64((dt.ToUniversalTime() - new DateTime(1970, 1, 1)).TotalMilliseconds); // get the difference between the server time and our current time long serverTimeDiff = dtms - CurrentTime; // update the Data object ServerTimeDiff = serverTimeDiff; LastServerTime = DateTime.Now.Ticks; } } // clear any sync error _lastSyncError = DateTime.MinValue; } } catch (WebException) { // don't retry for a while after error _lastSyncError = DateTime.Now; // set to zero to force reset ServerTimeDiff = 0; } }
/// <summary> /// Perform a request to the Steam WebAPI service /// </summary> /// <param name="url">API url</param> /// <param name="method">GET or POST</param> /// <param name="data">Name-data pairs</param> /// <param name="cookies">current cookie container</param> /// <returns>response body</returns> static string Request(string url, string method, NameValueCollection?data = null, CookieContainer?cookies = null, NameValueCollection?headers = null, int timeout = 0) { // create form-encoded data for query or body string query = (data == null ? string.Empty : string.Join("&", Array.ConvertAll(data.AllKeys, key => String.Format("{0}={1}", HttpUtility.UrlEncode(key), HttpUtility.UrlEncode(data[key]))))); if (string.Compare(method, "GET", true) == 0) { url += (url.IndexOf("?") == -1 ? "?" : "&") + query; } ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback((sender, certificate, chain, sslPolicyErrors) => { return(true); }); // call the server //HttpWebRequest request = GeneralHttpClientFactory(url); var request = GeneralHttpClientFactory.Create(url); request.Method = method; request.Accept = "text/javascript, text/html, application/xml, text/xml, */*"; request.ServicePoint.Expect100Continue = false; request.UserAgent = "Mozilla/5.0 (Linux; U; Android 4.1.1; en-us; Google Nexus 4 - 4.1.1 - API 16 - 768x1280 Build/JRO03S) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30"; request.AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip; request.Referer = COMMUNITY_BASE; // + "/mobilelogin?oauth_client_id=DE45CD61&oauth_scope=read_profile%20write_profile%20read_client%20write_client"; if (headers != null) { request.Headers.Add(headers); } if (cookies != null) { request.CookieContainer = cookies; } if (timeout != 0) { request.Timeout = timeout; } if (string.Compare(method, "POST", true) == 0) { request.ContentType = "application/x-www-form-urlencoded; charset=UTF-8"; request.ContentLength = query.Length; StreamWriter requestStream = new StreamWriter(request.GetRequestStream()); requestStream.Write(query); requestStream.Close(); } try { using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) { LogRequest(method, url, cookies, data, response.StatusCode.ToString() + " " + response.StatusDescription); // OK? if (response.StatusCode != HttpStatusCode.OK) { throw new WinAuthInvalidRequestException(string.Format("{0}: {1}", (int)response.StatusCode, response.StatusDescription)); } // load the response using (StreamReader responseStream = new StreamReader(response.GetResponseStream())) { string responseData = responseStream.ReadToEnd(); LogRequest(method, url, cookies, data, responseData); return(responseData); } } } catch (Exception ex) { LogException(method, url, cookies, data, ex); if (ex is WebException exception && exception.Response != null && ((HttpWebResponse)exception.Response).StatusCode == HttpStatusCode.Forbidden) { throw new WinAuthUnauthorisedRequestException(ex); } throw new WinAuthInvalidRequestException(ex.Message, ex); } }