在C# WPF应用程序中实现网络通信,可以使用多种方法,包括使用HttpClient
进行HTTP请求,或者使用TcpClient
和NetworkStream
进行TCP通信。下面是一个简单的示例,展示了如何使用HttpClient
进行HTTP GET请求。
添加必要的命名空间: 在你的XAML文件中添加必要的命名空间:
<Window x:Class="WpfApp.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApp"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<Button Content="Send Request" Click="Button_Click"/>
<TextBox x:Name="ResponseTextBox" Margin="10" TextWrapping="Wrap"/>
</Grid>
</Window>
添加代码逻辑:
在你的MainWindow.xaml.cs
文件中添加以下代码:
using System;
using System.Net.Http;
using System.Threading.Tasks;
using System.Windows;
namespace WpfApp
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private async void Button_Click(object sender, RoutedEventArgs e)
{
try
{
using (HttpClient client = new HttpClient())
{
HttpResponseMessage response = await client.GetAsync("https://api.example.com/data");
if (response.IsSuccessStatusCode)
{
string responseBody = await response.Content.ReadAsStringAsync();
ResponseTextBox.Text = responseBody;
}
else
{
ResponseTextBox.Text = $"Error: {response.StatusCode}";
}
}
}
catch (Exception ex)
{
ResponseTextBox.Text = $"Exception: {ex.Message}";
}
}
}
}
添加必要的命名空间: 在你的XAML文件中添加必要的命名空间:
<Window x:Class="WpfApp.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApp"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<Button Content="Send Request" Click="Button_Click"/>
<TextBox x:Name="ResponseTextBox" Margin="10" TextWrapping="Wrap"/>
</Grid>
</Window>
添加代码逻辑:
在你的MainWindow.xaml.cs
文件中添加以下代码:
using System;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
namespace WpfApp
{
public partial class MainWindow : Window
{
private const string ServerAddress = "127.0.0.1";
private const int ServerPort = 5000;
public MainWindow()
{
InitializeComponent();
}
private async void Button_Click(object sender, RoutedEventArgs e)
{
try
{
using (TcpClient client = new TcpClient(ServerAddress, ServerPort))
{
NetworkStream stream = client.GetStream();
byte[] requestData = Encoding.ASCII.GetBytes("Hello, Server!");
stream.Write(requestData, 0, requestData.Length);
byte[] responseData = new byte[1024];
int bytesRead = await stream.ReadAsync(responseData, 0, responseData.Length);
string response = Encoding.ASCII.GetString(responseData, 0, bytesRead);
ResponseTextBox.Text = response;
}
}
catch (Exception ex)
{
ResponseTextBox.Text = $"Exception: {ex.Message}";
}
}
}
}
以上示例展示了如何在C# WPF应用程序中使用HttpClient
进行HTTP GET请求,以及使用TcpClient
进行TCP通信。你可以根据具体需求选择合适的方法进行网络通信。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。