一、WebClient发送http请求
/// <summary> /// 使用WebClient 发送http请求 /// </summary> /// <param name="url"></param> /// <returns></returns> public string myWebCelin(string url) { WebClient clinet = new WebClient(); clinet.Encoding = Encoding.UTF8; var result = clinet.DownloadString(url); return result; }
二、HttpWebRequest 发送Get请求
/// <summary> /// Get请求 /// </summary> public static string HttpGet() { string url = "你要请求的接口"; string result = ""; HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest; request.Method = "GET"; request.ContentType = "text/html;charset=UTF-8"; HttpWebResponse response = request.GetResponse() as HttpWebResponse; Stream stream = response.GetResponseStream(); using (StreamReader reader = new StreamReader(stream, Encoding.UTF8)) { result = reader.ReadToEnd(); } return result; }