WebAPI中添加参数,并获取数据
public static string PostData(DataTable dt,int m) { string strPostData = ConfigurationManager.AppSettings["APIPost"].ToString(); //"http: //218.78.103.37:43333/api/pushData"; //dt=new Arichive().GETTbmeter_DB(); var dtjson =Getjson(dt,m);//转json Encoding encoding = Encoding.UTF8; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(strPostData); request.Method = "POST"; request.Accept = "text/html, application/xhtml+xml, */*"; request.ContentType = "application/json"; //Headers参数** request.Headers.Add("Name", "test"); request.Headers.Add("Token", token); //设置身份认证 // request.Credentials = CredentialCache.DefaultCredentials; //修改请求超时时间 //request.Timeout = 1000000; //Body参数** byte[] bytes = Encoding.UTF8.GetBytes(dtjson); request.ContentLength = bytes.Length; Stream strStream = request.GetRequestStream(); strStream.Write(bytes, 0, bytes.Length); strStream.Close(); HttpWebResponse response = (HttpWebResponse)request.GetResponse(); using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8)) { return reader.ReadToEnd(); } }