public async static Task<bool> Ping(ConnectionItem connectionItem) { JObject requestObject = new JObject( new JProperty("jsonrpc", "2.0"), new JProperty("id", 234), new JProperty("method", "JSONRPC.ping")); string requestData = requestObject.ToString(); HttpClientHandler handler = new HttpClientHandler(); HttpClient httpClient = new HttpClient(handler); // httpClient.Timeout = new TimeSpan(0, 0, 2); string uriString = "http://" + connectionItem.IpAddress + ":" + connectionItem.Port.ToString() + "/jsonrpc?request="; httpClient.BaseAddress = new Uri(uriString); HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, ""); request.Content = new StringContent(requestData); request.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json"); //Required to be recognized as valid JSON request. if (connectionItem.HasCredentials()) request.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", Convert.ToBase64String(Encoding.UTF8.GetBytes(String.Format("{0}:{1}", connectionItem.Username, connectionItem.Password)))); HttpResponseMessage response = await httpClient.SendAsync(request); string responseString = await response.Content.ReadAsStringAsync(); if (responseString.Length == 0) return false; //dynamic responseObject = JObject.Parse(responseString); JObject responseObject = JObject.Parse(responseString); bool isSuccessful = responseObject["result"].ToString() == "pong"; return isSuccessful; }
/// <summary> /// The methods provided in this section are simply used to allow /// NavigationHelper to respond to the page's navigation methods. /// <para> /// Page specific logic should be placed in event handlers for the /// <see cref="NavigationHelper.LoadState"/> /// and <see cref="NavigationHelper.SaveState"/>. /// The navigation parameter is available in the LoadState method /// in addition to page state preserved during an earlier session. /// </para> /// </summary> /// <param name="e">Provides data for navigation methods and event /// handlers that cannot cancel the navigation request.</param> protected override void OnNavigatedTo(NavigationEventArgs e) { GlobalVariables.CurrentTracker.SendView("EditConnectionPage"); currentConnection = e.Parameter as ConnectionItem; this.DataContext = currentConnection; this.navigationHelper.OnNavigatedTo(e); }
private async Task ConnectToServer(ConnectionItem connectionItem) { SetPageState(PageStates.Connecting); bool isSuccessful = false; try { isSuccessful = await JSONRPC.Ping(connectionItem); } catch { isSuccessful = false; } if (isSuccessful) { ConnectionManager.CurrentConnection = connectionItem; SettingsHelper.SetValue("RecentServerIP", connectionItem.IpAddress); Frame.Navigate(typeof(CoverPage)); } else { MessageDialog message = new MessageDialog("Could not reach the server.", "Connection Unsuccessful"); await message.ShowAsync(); SetPageState(PageStates.Ready); } }
public void RemoveConnectionItem(ConnectionItem itemToRemove) { ConnectionItems.Remove(itemToRemove); SaveConnections(); }
public void AddConnectionItem(ConnectionItem itemToAdd) { ConnectionItems.Add(itemToAdd); SaveConnections(); }
private async void SaveConnectionAppBarButton_Click(object sender, RoutedEventArgs e) { int port; if (!int.TryParse(PortTextBox.Text, out port)) { MessageDialog msg = new MessageDialog("Please enter a valid port number.", "Invalid Port"); await msg.ShowAsync(); return; } String ipAddress = IPTextBox.Text; if (NameTextBox.Text.Equals(string.Empty) || ipAddress.Equals(string.Empty)) { MessageDialog msg = new MessageDialog("Please enter valid name and server address", "Invalid Details"); await msg.ShowAsync(); return; } // ********************************************************************************************* // // Michele Mischitelli (11/07/2015) : // We should check for ip addresses starting with http:// in case people use dynamic DNS to always // be able to connect to their kodi at home. Https:// is not supported for now. const String httpsPrefix = "https://"; if (ipAddress.ToLower().StartsWith(httpsPrefix)) { var md = new MessageDialog("Https protocol is not currently supported.", "Invalid protocol"); await md.ShowAsync(); return; } const String httpPrefix = "http://"; if (ipAddress.ToLower().StartsWith(httpPrefix)) ipAddress = ipAddress.Substring(httpPrefix.Length); // ********************************************************************************************* // var newConnection = new ConnectionItem { ConnectionName = NameTextBox.Text, IpAddress = ipAddress, Port = port, Username = UsernameTextBox.Text, Password = PasswordTextBox.Text }; App.ConnectionsVM.AddConnectionItem(newConnection); Frame.GoBack(); }
/// The methods provided in this section are simply used to allow /// NavigationHelper to respond to the page's navigation methods. /// /// Page specific logic should be placed in event handlers for the /// <see cref="GridCS.Common.NavigationHelper.LoadState"/> /// and <see cref="GridCS.Common.NavigationHelper.SaveState"/>. /// The navigation parameter is available in the LoadState method /// in addition to page state preserved during an earlier session. protected override void OnNavigatedTo(NavigationEventArgs e) { currentConnection = e.Parameter as ConnectionItem; this.DataContext = currentConnection; navigationHelper.OnNavigatedTo(e); }