现在很多项目中需要调用其他公司的私人接口,这个时候我们不能暴露别人的接口,所以就要在自己的接口中调用,再整理输出为自己需要的数据
 public class Data
{
    public int errcode { get; set; }   //返回结果状态

    public string errmsg { get; set; }   //返回结果消息

    public object result { get; set; }  //返回结果
}
  
    [HttpPost]
    [Route("GetAccessToken")]   //最终调用的接口
    public JsonResult<Data> GetAccessToken()
    {
        Data rtd = new Data();
        try
        {
          

            //获取***的接口url
            string url = "要调用别人接口的地址";

            Dictionary<string, string> param = new Dictionary<string, string>();
            param.Add("Name", "张三");  //请求数据字段
            param.Add("Password","123456");//请求数据字段

            //调用接口,发送数据
            string returnData = DoPost(url, param);
            JObject joResult = (JObject)JsonConvert.DeserializeObject(returnData);
            if (joResult.GetString("code") == "200")  //返回数据中code字段是保存查询状态,200为成功
            {
                rtd.errcode = 0;
                rtd.errmsg = "操作成功!";
                rtd.result = joResult;  //返回json数据

            }
            else
            {
                rtd.errcode = 1;
                rtd.errmsg = "操作失败!";
                rtd.result = joResult;
            }
        }
        catch (Exception ex)
        {
            rtd.errcode = 1;
            rtd.errmsg = ex.Message.ToString();
        }
        return Json(rtd);
    }

这里是调用别人接口的方法
private string DoPost(string url, Dictionary<string, string> param)
    {
        StringBuilder sb = new StringBuilder();
        foreach (string key in param.Keys)
        {
            object value = param[key];
            sb.Append($"{key}={value}&");
        }

        string postData = sb.ToString();
        postData = postData.Substring(0, postData.Length - 1);

        byte[] byteArray = Encoding.UTF8.GetBytes(sb.ToString());

        HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
        request.Method = "POST";
        request.ContentType = "application/x-www-form-urlencoded";
        request.ContentLength = byteArray.Length;

        //打开request字符流
        Stream dataStream = request.GetRequestStream();
        dataStream.Write(byteArray, 0, byteArray.Length);
        dataStream.Close();

        //定义response为前面的request响应
        WebResponse response = request.GetResponse();

        //获取相应的状态代码
        //Console.WriteLine(((HttpWebResponse)response).StatusDescription);

        //定义response字符流
        dataStream = response.GetResponseStream();
        StreamReader reader = new StreamReader(dataStream);
        string responseFromServer = reader.ReadToEnd();//读取所有

        //关闭资源
        reader.Close();
        dataStream.Close();
        response.Close();
        return responseFromServer;
    }
    
 这个方法是把13位时间戳转换为日期形式的方法,根据需求是否使用
 private DateTime ConvertStringToDateTime(string timeStamp)
    {
        DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
        long lTime = long.Parse(timeStamp + "0000");
        TimeSpan toNow = new TimeSpan(lTime);
        return dtStart.Add(toNow);
    }
Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐