C# Get請求
阿新 • • 發佈:2020-05-29
一、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; }