webapi body 参数获取数据

public string HttpPost()
        {
            string strURL = "";
            DataTable dt = new Arichive().GetKEY();
            string JsonString = string.Empty;
            JsonString = "{\"appKey\":\"" + dt.Rows[0]["APPKEY"] + "\",\"appSecret\":\"" + dt.Rows[0]["APPSECRET"] + "\"}";
            // JsonString = JsonConvert.SerializeObject(dt);//转成json
            strURL = url + "/api/token";
            Encoding encoding = Encoding.UTF8;
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(strURL);
            request.Method = "POST";
            request.Accept = "text/html, application/xhtml+xml, */*";
            request.ContentType = "application/json";
            
                byte[] buffer = encoding.GetBytes(JsonString);
                request.ContentLength = buffer.Length;
                request.GetRequestStream().Write(buffer, 0, buffer.Length);
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
                {
                    return reader.ReadToEnd();
                }
           
        }

 

posted @ 2023-10-17 11:22  大黄人  阅读(99)  评论(0编辑  收藏  举报