//Method handling HTTP POST requests containg led configuration data public async Task <string> HandlePostRequest(IList <ILedConfiguration> data, HttpRequestsTypes requestType) { string responseText = null; try { var requestDataCollection = new FormUrlEncodedContent(GenerateKeyValuePair(data)); var responseMessage = await _client.PostAsync(GetServerUrl(requestType), requestDataCollection); responseText = await responseMessage.Content.ReadAsStringAsync(); Debug.WriteLine(responseText); if (String.IsNullOrEmpty(responseText)) { Debug.WriteLine("HTTP POST response text is null"); } } catch (Exception e) { Debug.WriteLine("HTTP POST request error: "); Debug.WriteLine(e); } return(responseText); }
//Method providing url for HTTP request taking in account wanted request type private string GetServerUrl(HttpRequestsTypes requestType) { if (requestType == HttpRequestsTypes.HttpGetSensorData) { return($"http://{_service.IpAddress}:{_service.IpPort}/?request=GETSens"); } else if (requestType == HttpRequestsTypes.HttpGetJoystickData) { return($"http://{_service.IpAddress}:{_service.IpPort}/?request=GETJoy"); } else { return($"http://{_service.IpAddress}:{_service.IpPort}/index.php"); } }
//Method handling HTTP GET request public async Task <string> HandleGetRequest(HttpRequestsTypes requestType) { string responseText = null; try { if (requestType == HttpRequestsTypes.HttpGetSensorData) { responseText = await _client.GetStringAsync(GetServerUrl(HttpRequestsTypes.HttpGetSensorData)); } else { responseText = await _client.GetStringAsync(GetServerUrl(HttpRequestsTypes.HttpGetJoystickData)); } } catch (Exception e) { Debug.WriteLine("Get Request Error: "); Debug.WriteLine(e); } return(responseText); }