详解C#.NET Core下的网络请求
在C#.NET Core下,我们可以使用多种方法进行网络请求,例如HttpClient、HttpWebRequest等等,下面将分别进行介绍。
HttpClient网络请求
创建HttpClient对象
使用HttpClient进行网络请求首先需要创建HttpClient对象。可以在以下几种方式中选取其中一种进行创建。
// 方式一:通过实例化HttpClient对象创建
HttpClient client = new HttpClient();
// 方式二:通过Http客户端工厂创建
IHttpClientFactory httpClientFactory = new HttpClientFactory();
HttpClient client = httpClientFactory.CreateClient();
// 方式三:通过DI容器注入HttpClient对象
public void ConfigureServices(IServiceCollection services)
{
services.AddHttpClient();
}
GET请求
HttpClient提供了GetAsync方法和GetStringAsync方法进行GET请求。
// 发送GET请求获取byte[]
HttpResponseMessage response = await client.GetAsync("https://www.example.com");
byte[] byteData = await response.Content.ReadAsByteArrayAsync();
// 发送GET请求获取string
string strData = await client.GetStringAsync("https://www.example.com");
POST请求
HttpClient提供了PostAsync方法对POST请求进行处理。
// 发送POST请求
string postContent = "id=1&name=John";
HttpContent content = new StringContent(postContent, Encoding.UTF8, "application/x-www-form-urlencoded");
HttpResponseMessage response = await client.PostAsync("https://www.example.com", content);
string result = await response.Content.ReadAsStringAsync();
HttpWebRequest网络请求
创建HttpWebRequest对象
使用HttpWebRequest进行网络请求首先需要创建HttpWebRequest对象。可以通过以下代码实现:
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://www.example.com");
GET请求
使用HttpWebRequest发送GET请求时,我们可以直接使用Response.GetResponseStream()方法获取响应流,再通过StreamReader将流转换为字符串/字节数组。
// 发送GET请求获取byte[]
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream responseStream = response.GetResponseStream();
byte[] byteData;
using (MemoryStream ms = new MemoryStream())
{
responseStream.CopyTo(ms);
byteData = ms.ToArray();
}
// 发送GET请求获取string
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream responseStream = response.GetResponseStream();
StreamReader sr = new StreamReader(responseStream);
string strData = sr.ReadToEnd();
POST请求
使用HttpWebRequest发送POST请求可以通过以下代码实现:
// 发送POST请求
string postContent = "id=1&name=John";
byte[] postData = Encoding.UTF8.GetBytes(postContent);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = postData.Length;
using (Stream stream = request.GetRequestStream())
{
stream.Write(postData, 0, postData.Length);
}
HttpWebResponse response = request.GetResponse() as HttpWebResponse;
StreamReader sr = new StreamReader(response.GetResponseStream());
string result = sr.ReadToEnd();
示例
下面通过一个简单的示例,展示如何使用HttpClient发送GET请求并解析响应。
HttpClient client = new HttpClient();
HttpResponseMessage response = await client.GetAsync("https://www.example.com");
byte[] byteData = await response.Content.ReadAsByteArrayAsync();
string strData = Encoding.UTF8.GetString(byteData);
Console.WriteLine("Response: " + strData);
下面通过一个简单的示例,展示如何使用HttpWebRequest发送POST请求并解析响应。
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://www.example.com");
string postContent = "id=1&name=John";
byte[] postData = Encoding.UTF8.GetBytes(postContent);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = postData.Length;
using (Stream stream = request.GetRequestStream())
{
stream.Write(postData, 0, postData.Length);
}
HttpWebResponse response = request.GetResponse() as HttpWebResponse;
StreamReader sr = new StreamReader(response.GetResponseStream());
string result = sr.ReadToEnd();
Console.WriteLine("Response: " + result);
以上是关于C#.NET Core下的网络请求的详细攻略,希望对您有所帮助。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:详解c# .net core 下的网络请求 - Python技术站