c# 调apicontroller

public string Post(ModelCs mod) {
string formData = Newtonsoft.Json.JsonConvert.SerializeObject(mod);
byte[] postData = Encoding.UTF8.GetBytes(formData);
HttpWebRequest request = WebRequest.Create("http://localhost:9527/app/CsAPI/QueryData") as HttpWebRequest;
request.Method = "POST";
request.KeepAlive = false;
request.AllowAutoRedirect = true;
request.ContentType = "application/json; charset=utf-8";
request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)";
request.ContentLength = postData.Length;
System.IO.Stream outputStream = request.GetRequestStream();
outputStream.Write(postData, 0, postData.Length);
outputStream.Close();
HttpWebResponse response;
Stream responseStream;
StreamReader reader;
string srcString;
response = request.GetResponse() as HttpWebResponse;
responseStream = response.GetResponseStream();
reader = new System.IO.StreamReader(responseStream, Encoding.GetEncoding("UTF-8"));
srcString = reader.ReadToEnd();
string result = srcString; //返回值赋值
reader.Close();
return result;
}

转载于:https://www.cnblogs.com/zhang-wenbin/p/7065686.html

你可能感兴趣的:(c#,json)