C# winform 系统中获取接口api信息的HttpClient和HttpWebResponse方法

在这里插入图片描述

使用HttpClient 访问接口

//post请求-- auth为Basic Auth
private  void getdata() {
    string BUKRS = "1000";
    string WERKS = "";
    DateTime MON = DateTime.Now;
    string ZDEPA = "";
    string ZREPTYPE = "1";
    string url = "http://xxxxxx";
    string username = "xxxxx";
    string password = "xxxxxxx";
    // 将用户名和密码组合成 Basic Authentication 的凭据
    string credentials = Convert.ToBase64String(Encoding.ASCII.GetBytes($"{username}:{password}"));
    string[] DAPA = new string[] { "DB01", "DB02" };
    foreach (string item in DAPA)
    {
        ZDEPA = item;
        for (int i = 1; i < 20; i++)
        {
            string ZMON = MON.AddMonths(-i).ToString("yyyyMM");
            //Dictionary
            //定义一个键值对集合
            Dictionary<string, string> dictionary = new Dictionary<string, string>();
            dictionary.Add("BUKRS", BUKRS);
            dictionary.Add("WERKS",WERKS);
            dictionary.Add("ZMON",ZMON);
            dictionary.Add("ZDEPA", ZDEPA);
            dictionary.Add("ZREPTYPE",ZREPTYPE);
            //将键值对转为JSON
            string json = JsonConvert.SerializeObject(dictionary);
            // 设置 POST 请求的内容
            var content = json;
            // 设置请求头
            using (HttpClient httpClient = new HttpClient()) { 
              httpClient.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", credentials);

              HttpContent httpContent = new StringContent(content, Encoding.UTF8);
              httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
              Uri address = new Uri(url);
              var response = httpClient.PostAsync(address, httpContent).Result.Content.ReadAsStringAsync().Result;//返回值
            }

        }
    }
}

使用HttpWebRequest和HttpWebResponse获取数据

private async void SAPRKGBINOFO() {
    string BUKRS = "1000";
    string WERKS = "";
    DateTime MON = DateTime.Now;
    string ZDEPA = "";
    string ZREPTYPE = "1";
    string url = "xxxx";
    string username = "xxxx";
    string password = "xxxx";
    // 将用户名和密码组合成 Basic Authentication 的凭据
    string credentials = Convert.ToBase64String(Encoding.ASCII.GetBytes($"{username}:{password}"));
    string[] DAPA = new string[] { "DB01", "DB02" };
    foreach (string item in DAPA)
    {
        ZDEPA = item;
        for (int i = 1; i < 20; i++)
        {
            string ZMON = MON.AddMonths(-i).ToString("yyyyMM");
            //Dictionary
            //定义一个键值对集合
            Dictionary<string, string> dictionary = new Dictionary<string, string>();
            dictionary.Add("BUKRS", BUKRS);
            dictionary.Add("WERKS",WERKS);
            dictionary.Add("ZMON",ZMON);
            dictionary.Add("ZDEPA", ZDEPA);
            dictionary.Add("ZREPTYPE",ZREPTYPE);
            //将键值对转为JSON
            string json = JsonConvert.SerializeObject(dictionary);
            // 设置 POST 请求的内容
            var content = json;
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            request.Method = "POST";
            request.ContentType = "text/html; charset=utf-8";
            request.Headers.Add("Authorization", "Basic " + credentials);
            using (StreamWriter dataStream = new StreamWriter(request.GetRequestStream()))
            {
                dataStream.Write(content);
                dataStream.Close();
            }
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            string encoding = response.ContentEncoding;
            if (encoding == null || encoding.Length < 1)
            {
                encoding = "UTF-8"; //默认编码  
            }
            StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding(encoding));
            string retString = reader.ReadToEnd();//返回值
            //解析josn
            JObject jo = JObject.Parse(retString);
        }
    }
}