private async void Test_Click(object sender, RoutedEventArgs e) { if (_threadRunning) { SetTestButtonText("Stopping Test..."); _thread.CancelAsync(); return; } ClearBrowser(); Pivot.SelectedIndex = 0; var data = new ResolverData() { Host = Cell.SelectedItem.ToString(), Method = Method.SelectedItem.ToString(), Cell = Cell.SelectedItem.ToString(), Payload = Payload.Text, Type = Type.Text, ClientData = ClientData.Text, GetCell = GetCell.IsChecked ?? false, RepeatDelay = Convert.ToInt32(Delay.Text) }; if (Repeat.IsChecked ?? false) { SetTestButtonText("Stop Test"); _threadRunning = true; _thread.RunWorkerAsync(data); return; } await StartSingleRequest(data); }
private async Task <string> CallResolver(ResolverData data) { if (data.Method == "Payoff") { return(await Resolver.Payoff(data)); } return(await Resolver.IsAvailable(data)); }
/// <summary> /// Calls the Payoff service method /// </summary> public static async Task <string> Payoff(ResolverData data) { var handler = new HttpClientHandler(); handler.PreAuthenticate = handler.SupportsPreAuthenticate(); using (var client = new HttpClient(handler)) { try { // formulate full url string url = string.Format(_payoffPathV2, data.Host, data.Payload, data.Type); AppendClientData(ref url, data); var now = DateTime.UtcNow; client.DefaultRequestHeaders.Date = now; string body = SerializeObject(CreateMetricV2()).ToString(); int lenBody = Encoding.UTF8.GetByteCount(body); var content = new StringContent(body); content.Headers.ContentType = MediaTypeHeaderValue.Parse(_contentType); // set user credentials in Authorization header string authInfo = CreateAuthorizationHeader(url, "POST", lenBody, _contentType, now); client.DefaultRequestHeaders.TryAddWithoutValidation("Authorization", authInfo); // make request HttpResponseMessage response; try { Elapsed.Restart(); response = await client.PostAsync(url, content); } finally { Elapsed.Stop(); } StatusCode = response.StatusCode; if (!response.IsSuccessStatusCode) { return("StatusCode: " + StatusCode); } var result = response.Content.ReadAsStringAsync().Result; return(JsonIO.FormatFragment(result)); } catch (Exception e) { var error = string.Format("Exception: " + e.Message); if (e.InnerException != null) { error += string.Format("\nInner Exception: " + e.InnerException.Message); } return(error); } } }
/// <summary> /// Appends the clientdata query param if specified /// </summary> private static void AppendClientData(ref string url, ResolverData data) { if (data.GetCell) { url += string.Format(_clientDataFmtV2, "usecellname"); } else if (!string.IsNullOrWhiteSpace(data.ClientData)) { url += string.Format(_clientDataFmtV2, data.ClientData); } }
private async Task StartSingleRequest(ResolverData data) { string response = await CallResolver(data); if (Repeat.IsChecked ?? false) { Browser.NavigateToString(TimedOutput(response)); } else { Browser.NavigateToString(FullOutput(response)); } }
/// <summary> /// Calls the IsAvailable service method /// </summary> public static async Task <string> IsAvailable(ResolverData data) { using (var client = new HttpClient()) { try { // formulate full url string url = string.Format(_availPathTestDb, data.Host); AppendClientData(ref url, data); // make request HttpResponseMessage response; try { Elapsed.Restart(); response = await client.GetAsync(url); } finally { Elapsed.Stop(); } StatusCode = response.StatusCode; if (!response.IsSuccessStatusCode) { return("StatusCode: " + StatusCode); } var result = response.Content.ReadAsStringAsync().Result; return(JsonIO.FormatFragment(result)); } catch (Exception e) { var error = string.Format("Exception: " + e.Message); if (e.InnerException != null) { error += string.Format("\nInner Exception: " + e.InnerException.Message); } return(error); } finally { Elapsed.Stop(); } } }
public ResolverData GetResolverData(string host, int port, string user, string passwd, int contestId) { conn = new MySqlConnection { ConnectionString = $"server={host};port={port};uid={user};pwd={passwd};database=db_nenu_oj" }; conn.Open(); var users = GetUsers(contestId); var(problemCount, solutions) = GetSolutions(contestId); conn.Close(); var contest = new ResolverData { ProblemCount = problemCount, Users = users, Solutions = solutions }; return(contest); }