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)); } }
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)); } }
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); }
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)); }