1. 程式人生 > >c#通過http協議讀取字串

c#通過http協議讀取字串

 下面是c#通過http讀取字串的方法:

           string urlstr = @"http://.../test.json";


            Uri url = new Uri(urlstr);
            HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(url);
            myHttpWebRequest.Accept = "application/xml";
            myHttpWebRequest.Headers.Add("X-***-Token-Id", "***");
            myHttpWebRequest.Method = "GET";
            HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();
            Stream stream = myHttpWebResponse.GetResponseStream();

            StreamReader reader = new StreamReader(stream, Encoding.UTF8);

           //輸出字串資訊

            string szInfo= reader.ReadToEnd();
            myHttpWebResponse.Close();