在C#中获取消息有多种方法,以下是其中的一些实现:
using System.Net.WebClient;
using (var client = new WebClient())
{
string response = client.DownloadString("https://example.com");
Console.WriteLine(response);
}
using System.Net;
using (WebRequest request = WebRequest.Create("https://example.com"))
{
using (WebResponse response = request.GetResponse())
{
using (StreamReader reader = new StreamReader(response.GetResponseStream()))
{
string responseText = reader.ReadToEnd();
Console.WriteLine(responseText);
}
}
}
using System.Net.Http;
using (HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://example.com"))
{
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
using (StreamReader reader = new StreamReader(response.GetResponseStream()))
{
string responseText = reader.ReadToEnd();
Console.WriteLine(responseText);
}
}
}
using System.Net.WebSockets;
using (var socket = new WebSocket("wss://example.com"))
{
string response = await socket.ReceiveTextAsync();
Console.WriteLine(response);
}
以上方法可以根据不同的场景进行选择,具体的实现可以根据实际需求进行优化。
领取专属 10元无门槛券
手把手带您无忧上云