Beispiel #1
0
        public async Task <StationsResonse> GetStationsAsync(UzContext context, string term)
        {
            string normalizedTerm = HttpUtility.UrlEncode(HttpUtility.UrlDecode(term));

            using (CookieSupportatbleHttpClient client = CreateHttpClient(context)) {
                HttpResponseMessage response = await client.GetAsync(UrlStation + $"?term={normalizedTerm}");

                string rawStations = await response.Content.ReadAsStringAsync();

                return(ResponseFormatter.FormatStations(rawStations));
            }
        }
Beispiel #2
0
        public async Task <UzContext> GetUZContextAsync()
        {
            using (CookieSupportatbleHttpClient client = CreateHttpClient()) {
                HttpResponseMessage response = await client.GetAsync(BaseUrl);

                string mainPageHtml = await response.Content.ReadAsStringAsync();

                string           token   = TokenDecoder.Decode(mainPageHtml);
                CookieCollection cookies = client.ReadCookies(response);
                return(new UzContext(token, cookies));
            }
        }
Beispiel #3
0
        private CookieSupportatbleHttpClient CreateHttpClient(UzContext context = null)
        {
            var httpClient = new CookieSupportatbleHttpClient();

            if (context != null)
            {
                httpClient.DefaultRequestHeaders.Add("GV-Ajax", "1");
                httpClient.DefaultRequestHeaders.Add("GV-Referer", BaseUrl);
                httpClient.DefaultRequestHeaders.Add("GV-Token", context.Token);
                httpClient.SetCookie(context.Cookie);
            }

            return(httpClient);
        }
Beispiel #4
0
        private async Task <T> ExecutePostRequestAsync <T>(UzContext context, string url, FormUrlEncodedContent content, Func <string, T> formatFunc)
        {
            string raw;

            using (CookieSupportatbleHttpClient client = CreateHttpClient(context)) {
                HttpResponseMessage response;
                try {
                    response = await client.PostAsync(url, content);
                } catch (HttpRequestException e) {
                    return(Activator.CreateInstance <T>());
                }
                raw = await response.Content.ReadAsStringAsync();
            }
            return(FormatRequestResult(raw, formatFunc));
        }