發表文章

目前顯示的是有「Get」標籤的文章

C# 使用Get取得網頁執行結果資料(資料格式為JSON) [HttpWebRequest]

C# 使用Get取得網頁執行結果資料(資料格式為JSON) [HttpWebRequest] void Main() { string apiUrl = "http://aaa/bbb/ccc?x=11&y=222"; string Msg = ""; HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(apiUrl); req.Method = "GET"; req.ContentType = "application/json"; req.Timeout = 3000000; //以毫秒為單位 using (HttpWebResponse wr = (HttpWebResponse)req.GetResponse()) { if (wr.StatusCode == HttpStatusCode.OK) { using (Stream stream = wr.GetResponseStream()) using (StreamReader reader = new StreamReader(stream)) { Msg = reader.ReadToEnd(); } } } RDataSS RData = JsonConvert.DeserializeObject<RDataSS>(Msg); RData.Dump(); } // Define other methods and classes here public class RDataSS { public string ReturnCode { get; set; } public string Message { get; set; } public string StorageID { get; set; } }  

C# 使用Get取得網頁執行結果資料(資料格式為JSON) [HttpClient ]

C# 使用Get取得網頁執行結果資料(資料格式為JSON) [HttpClient]  void Main() { string apiUrl = "http://aaa/bbb/ccc?x=11&y=222"; using (HttpClient client = new HttpClient()) { // 執行GET請求並取得回應 HttpResponseMessage response = client.GetAsync(apiUrl).Result; // 確認回應是否成功 response.EnsureSuccessStatusCode(); // 讀取回應內容並解析成字串 string responseBody = response.Content.ReadAsStringAsync().Result; responseBody.Dump(); // 將JSON字串轉換成物件,假設回傳的JSON結構是一個物件的話 // 如果是陣列或其他結構,請使用相對應的方法進行解析 // 你需要使用Json.NET或System.Text.Json等套件進行解析 // 這裡以Json.NET為例 RDataSS responseObject = Newtonsoft.Json.JsonConvert.DeserializeObject<RDataSS>(responseBody); responseObject.Dump(); } } // Define other methods and classes here public class RDataSS { public string ReturnCode { get; set; } public string Message { get; set; } public string AAAA { get; set; } }