API调用小练习:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;
using System.Web;
using System.Web.Mvc;
namespace MyWeb.Controllers
{
public class HomeController : Controller
{
//如何使用POST调用API接口步骤:
//1.确认调用接口地址: string url =”xxxx”
//2.赋值要传递给接口的参数:
//字典: Dictionary<string, string> D = new Dictionary<string, string>(); 或 List<KeyValuePair<stirng, string>> L= new List<KeyValuePair<string, string>>();
//3.创建HttpClient类对象
//4.创建FormUrlEncodedContent对象得到参数实例
//5.使用httpClient对象里的方法拿到,接口处理结果
public ActionResult Index()
{
string urlStirng = "http://localhost:5788/weatherforecast";
HttpClient experssClient=new HttpClient();
Dictionary<string, string> exDictionary = new Dictionary<string, string>();
//exDictionary["type"] = "zhongtong";
//exDictionary["postid"] = "73123087952547";
FormUrlEncodedContent excontent = new FormUrlEncodedContent(exDictionary);
var excall = experssClient.GetAsync(urlStirng).Result;
var excall2 = experssClient.PostAsync(urlStirng, excontent).Result;
//var exresult = Excall.Result;
var rqCode = excall.StatusCode;// 响应码
var rqHeader = excall.Headers;//响应报文头
var rqConent = excall.Content;//响应报文体
var responsemsg = excall.Content.ReadAsStringAsync().Result;//get请求结果
var responsemsg2 = excall2.Content.ReadAsStringAsync().Result;//post请求结果
return View();
}
}
}
调用的接口代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
namespace Learn.ApiUse.Controllers
{
[ApiController]
[Route("[controller]")]
public class WeatherForecastController : ControllerBase
{
private static readonly string[] Summaries = new[]
{
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
};
private readonly ILogger<WeatherForecastController> _logger;
public WeatherForecastController(ILogger<WeatherForecastController> logger)
{
_logger = logger;
}
[HttpGet]
public IEnumerable<WeatherForecast> Get()
{
var rng = new Random();
return Enumerable.Range(1, 5).Select(index => new WeatherForecast
{
Date = DateTime.Now.AddDays(index),
TemperatureC = rng.Next(-20, 55),
Summary = Summaries[rng.Next(Summaries.Length)],
Remark = "这是Get请求"
})
.ToArray();
}
[HttpPost]
public IEnumerable<WeatherForecast> Post()
{
var rng = new Random();
return Enumerable.Range(1, 5).Select(index => new WeatherForecast
{
Date = DateTime.Now.AddDays(index),
TemperatureC = rng.Next(-20, 55),
Summary = Summaries[rng.Next(Summaries.Length)],
Remark="这是Post请求"
})
.ToArray();
}
}
}
简单了解些其他:
什么是API接口:APPlication Programming Interface,既应用程序编程接口;
一般包括Method方法:请求方法、URL:唯一资源定位符、Paramas:参数、Authorization:认证方法、Headers:消息头、Body:消息体
Get请求: 从指定资源请求数据,查询字符串是在get请求的url中发送的;列如:test/demo_form.asp.net?name1=value&name2=value2 在约定中,参数是在?后面,用&分割。Get其他说明:
①get请求可以被缓存:(IE会缓存所有资源;谷歌和火狐浏览只缓存静态资源;数据则不会)
②get请求可以保存在浏览器的历史记录中
③get请求被收藏
④get请求不应再处理敏感数据中使用
⑤get请求有长度限制
⑥get请求只应当用于取回数据
Post请求: 查询字符串是在post请求的http消息的主体中发送的;post 的请求中多了From Data(body的中一种表单请求类型)。Header:主要存放一些cookie信息 bod:主要存放一些POST提交数据;如:username:xxx
POST其他说明:
①POST请求不会被缓存
②POST请求不会被保留在浏览器的历史记录中
③POST请求不会被收藏
④POST请求对数据长度没有限制