encode)){ return reader.ReadToEnd();}}

时间:2021-12-14 07:57:03

每每会有用到许多后台请求接口取数据的情况,可能很多初学者不了解,不知道如何去操纵,特贴此详例,仅供参考:

//Url为请求路径 public static string RequestUrl(string url, System.Text.Encoding encode) { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(new Uri(url)); request.Method = "Get"; request.ContentType = "application/x-www-form-urlencoded"; HttpWebResponse response = (HttpWebResponse)request.GetResponse(); using (System.IO.StreamReader reader = new System.IO.StreamReader(response.GetResponseStream(), encode)) { return reader.ReadToEnd(); } }