方法
public static List<T> HttpGet<T>(string url) where T : EntityBase
{
Encoding encoding = Encoding.UTF8;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "GET";
request.Accept = "text/html, application/xhtml+xml, */*";
request.ContentType = "application/json";
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
{
List<T> ts = JsonHelper.JsonToObject<List<T>>(reader.ReadToEnd());
return ts;
}
}
调用
List<MyRoomEntity> list = HttpGet<MyRoomEntity>(url);
List<MyContractEntity> list = HttpGet<MyContractEntity>(url);
本文介绍了一种使用C#进行HTTP GET请求的方法,并通过JSONHelper将返回的JSON字符串转换为指定类型的实体列表。示例展示了如何调用该方法获取不同类型的数据。
1580

被折叠的 条评论
为什么被折叠?



