netcore3.1
控制台
安装包如下
F12先看你要请求的的页面(下图是我要请求的页面接口)
因为大部分信息都是固定格式,先看有用信息,下图有用信息是:
Request URL :https://account.cnblogs.com/api/user/info(这个是必填的请求路径);
Request Method:Get(请求必填的类型)
cookie:你自己登录后的值
Get请求代码例子
public static void Main(string[] args) { Run().GetAwaiter().GetResult(); } public static async Task Run() { var serviceProvider = new ServiceCollection().AddHttpClient().BuildServiceProvider(); IHttpClientFactory _httpClientFactory = serviceProvider.GetService<IHttpClientFactory>(); using var client = _httpClientFactory.CreateClient(); using var request = new HttpRequestMessage(); request.Method = HttpMethod.Get; request.Headers.Add("cookie", "当前页面请求的cookie值"); //路径可以随便填(填你自己需要的),我是因为页面有这个路径我就直接用了,哈哈哈 request.RequestUri = new Uri("https://account.cnblogs.com/api/user/info", UriKind.RelativeOrAbsolute); try { var response = await client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, System.Threading.CancellationToken.None); if (response.StatusCode != HttpStatusCode.OK) { Console.WriteLine($"失败"); Console.ReadLine(); } ////str里面的值是你拿到的接口返回值 var str =await response.Content.ReadAsStringAsync(); Console.WriteLine($"{str}"); Console.ReadLine(); } catch (Exception ex) { Console.WriteLine($"错误信息{ex}"); Console.ReadLine(); } }
注意:Get类型的请求参数直接跟在路径后面,我举例的这个路径没有参数,如果这个请求有参数,是(路径?参数&参数)这种格式,例:https://account.cnblogs.com/api/user/info?a=1&b=2&c=3
页面返回值如下图
得到的返回值如下图
Post请求例子
f12看到页面请求的接口
将请求路径和类型复制备用
post请求的代码如下
public static void Main(string[] args) { Run().GetAwaiter().GetResult(); } public static async Task Run() { var parameters1 = new { title = "测试1454", description = "测试1454", visible = true }; var json = JsonConvert.SerializeObject(parameters1); var serviceProvider = new ServiceCollection().AddHttpClient().BuildServiceProvider(); IHttpClientFactory _httpClientFactory = serviceProvider.GetService<IHttpClientFactory>(); using var client = _httpClientFactory.CreateClient(); using var request = new HttpRequestMessage(); request.Method = HttpMethod.Post; request.Headers.Add("cookie", "当前页面 Request Headers里面cookie值"); var httpContent = new StringContent(json, Encoding.UTF8); httpContent.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json"); request.Content = httpContent; //路径可以随便填(填你自己需要的),我是因为页面有这个路径我就直接用了,哈哈哈 request.RequestUri = new Uri("https://i.cnblogs.com/api/category/blog/1", UriKind.RelativeOrAbsolute); try { var response = await client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, System.Threading.CancellationToken.None); if (response.StatusCode != HttpStatusCode.OK) { Console.WriteLine($"失败"); Console.ReadLine(); } //str里面的值是你拿到的接口返回值 var str = await response.Content.ReadAsStringAsync(); Console.WriteLine($"{str}"); Console.ReadLine(); } catch (Exception ex) { Console.WriteLine($"错误信息{ex}"); Console.ReadLine(); } }
注意:post请求的参数和get参数不同,不是直接跟在路径?后面的;post的参数是放在body体里的,如下图
友情提示:如果你的请求失败,你可以先用Postman或者其他测试接口的软件先测试下你要请求的接口是不是可以请求到,有没有返回值。
标签:Console,请求,request,var,new,IHttpClientFactory,页面 From: https://www.cnblogs.com/ererjie520/p/16591060.html