.net后台调用API接口
作者:互联网
string dd = model.Fatherid;
string serviceAddress = "http://public.data.test.cn/api/PropertyData/GetFloorBuilding";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(serviceAddress);
request.Method = "POST";
request.ContentType = "application/json";
string strContent = @"{ ""Fatherid"": " + dd + "}";//参数
using (StreamWriter dataStream = new StreamWriter(request.GetRequestStream()))
{
dataStream.Write(strContent);//写入参数
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();
//JObject jo = JObject.Parse(retString);
//解析josn
GetFloorBuildingResult jo1 = JsonConvert.DeserializeObject<GetFloorBuildingResult>(retString);
return jo1;
//GetFloorBuildingResult 返回结果
标签:dataStream,string,encoding,request,接口,retString,API,net,response 来源: https://www.cnblogs.com/tfeblog/p/10619301.html