在利用C# 叫用 Web API 時, 須要透過HttpWebRequest 去傳送request 和HttpWebResponse 去收return 回來的data. 以下是部份測試代碼.
HttpWebRequest webRequest = HttpWebRequest.Create(TargetWebsite) as HttpWebRequest; webRequest.Method = "GET"; // "GET" / "POST" using (HttpWebResponse webResponse = webRequest.GetResponse() as HttpWebResponse) { if (webResponse.StatusCode == HttpStatusCode.OK) { using (StreamReader myStreamReader = new StreamReader(webResponse.GetResponseStream())) { string data = myStreamReader.ReadToEnd(); Console.WriteLine("data:" + data); } } }
Reference
Leave a Reply