//定义属性:TargetUrl参数是目标接口的URL,RequestDataArray参数是要发送的请求数据列表
public class ToResponseBody
{
[JsonPropertyName("TargetUrl")]
public string TargetUrl { get; set; } = "";
[JsonPropertyName("RequestDataArray")]
public string[] RequestDataArray { get; set; } = null;
}
public class ApiReturnData
{
public int code { get; set; } = 0;
public string msg { get; set; } = "";
public dynamic data { get; set; } = new ExpandoObject();
}
(1)大家首先进入以下网址:这个网址介绍了 用户如果要调用百度账号的 API 接口,需要通过百度开发者中心的身份验证,具体步骤进入网址中查看(里面介绍得非常详细):
地址:https://tongji.baidu.com/api/manual/Chapter2/openapi.html
(2)接着再进入接口网址:
地址:https://tongji.baidu.com/api/manual/Chapter1/overview_getTimeTrendRpt.html
其中里面有很多接口:
以其中 【网站概况(趋势数据)】为例:里面包含请求的关键参数与接口地址
以上就是一个基本介绍,详细内容大家可以在官网里面仔细查看!
[AllowAnonymous]
[HttpPost("GetResponseBody")]
public async ValueTask<ApiReturnData> GetResponseBody([FromBody] ToResponseBody request)
{
//使用HttpClient类:HttpClient类是一个强大的HTTP客户端,可以用于发送HTTP请求并接收响应。您可以使用它来实现代理功能,发送请求到目标接口并返回响应。
//代码中的targetUrl参数是目标接口的URL,requestData参数是要发送的请求数据
try
{
using (HttpClient client = new HttpClient())
{
// 创建HttpRequestMessage对象,设置HTTP方法和请求内容
HttpRequestMessage requests = new HttpRequestMessage(HttpMethod.Post, request.TargetUrl);
/*
foreach (string element in request.RequestDataArray)
{
Console.WriteLine(element);
}*/
string metrics = "";
// 将请求的metrics参数进行拼接
for (int i = 4;i< request.RequestDataArray.Length; i++)
{
metrics =request.RequestDataArray[i] + "," + metrics;
}
string reqdata = String.Format("&site_id={0}&method={1}&start_date={2}&end_date={3}&metrics={4}", request.RequestDataArray[0], request.RequestDataArray[1], request.RequestDataArray[2], request.RequestDataArray[3], metrics);
requests.Content = new StringContent(reqdata, System.Text.Encoding.UTF8, "application/json");
// 发送HTTP请求并获取响应
HttpResponseMessage response = await client.SendAsync(requests);
// 确保请求成功
response.EnsureSuccessStatusCode();
// 读取响应内容
string responseData = await response.Content.ReadAsStringAsync();
// 创建返回数据对象rt
ApiReturnData rt = new ApiReturnData();
rt.code = 0;
rt.msg = "";
rt.data.ResponseBody = responseData;
return rt;
}
}
catch (Exception ex)
{
ApiReturnData rt = new ApiReturnData();
rt.code = 0;
rt.msg = "";
rt.data.ResponseBody = ex.Message;
return rt;
}
}
结果如下:
参数输入
得到的数据:
[AllowAnonymous]
[HttpPost("GetResponseBody1")]
public async ValueTask<ApiReturnData> GetResponseBody1([FromBody] ToResponseBody request)
{
try
{
using (WebClient client = new WebClient())
{
// 设置请求头
client.Headers[HttpRequestHeader.ContentType] = "application/json";
string metrics = "";
// 将请求的metrics参数进行拼接
for (int i = 4; i < request.RequestDataArray.Length; i++)
{
metrics = request.RequestDataArray[i] + "," + metrics;
}
string reqdata = String.Format("&site_id={0}&method={1}&start_date={2}&end_date={3}&metrics={4}", request.RequestDataArray[0], request.RequestDataArray[1], request.RequestDataArray[2], request.RequestDataArray[3], metrics);
// 发送HTTP请求并获取响应
string responseData = await client.UploadStringTaskAsync(request.TargetUrl, "POST", reqdata);
ApiReturnData rt = new ApiReturnData();
rt.code = 0;
rt.msg = "";
rt.data.ResponseBody = responseData;
return rt;
}
}
catch (Exception ex)
{
ApiReturnData rt = new ApiReturnData();
rt.code = 0;
rt.msg = "";
rt.data.ResponseBody = ex.Message;
return rt;
}
}
结果如下:
[AllowAnonymous]
[HttpPost("GetResponseBody2")]
public async ValueTask<ApiReturnData> GetResponseBody2([FromBody] ToResponseBody request)
{
try
{
// 创建HttpWebRequest对象
HttpWebRequest requests = (HttpWebRequest)WebRequest.Create(request.TargetUrl);
requests.Method = "POST";
requests.ContentType = "application/json";
string metrics = "";
// 将请求的metrics参数进行拼接
for (int i = 4; i < request.RequestDataArray.Length; i++)
{
metrics = request.RequestDataArray[i] + "," + metrics;
}
string reqdata = String.Format("&site_id={0}&method={1}&start_date={2}&end_date={3}&metrics={4}", request.RequestDataArray[0], request.RequestDataArray[1], request.RequestDataArray[2], request.RequestDataArray[3], metrics);
// 将请求内容写入请求流
using (StreamWriter writer = new StreamWriter(requests.GetRequestStream()))
{
writer.Write(reqdata);
}
// 发送HTTP请求并获取响应
using (HttpWebResponse response = (HttpWebResponse)await requests.GetResponseAsync())
{
// 读取响应流
using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
{
string responseData = await reader.ReadToEndAsync();
ApiReturnData rt = new ApiReturnData();
rt.code = 0;
rt.msg = "";
rt.data.ResponseBody = responseData;
return rt;
}
}
}
catch (Exception ex)
{
ApiReturnData rt = new ApiReturnData();
rt.code = 0;
rt.msg = "";
rt.data.ResponseBody = ex.Message;
return rt;
}
}
结果如下:
以上就是本节的一个简单介绍,大家可以借鉴参考一下,有什么不足的地方大家指出一下!