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(); } }
标签:webapi,body,JsonString,string,Encoding,buffer,request,获取数据,dt From: https://www.cnblogs.com/lyq666666/p/17769276.html