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; }
}
留言
張貼留言