C# winform后台获取远程webservice Json 数据

private void getjson(object o)
        {
            try
            {
                HttpWebRequest webRequest;
                string url ="";
                webRequest = (HttpWebRequest)WebRequest.Create(url);
                webRequest.Method = "GET";
                webRequest.ContentType = "application/x-www-form-urlencoded";

                using (HttpWebResponse response = (HttpWebResponse)webRequest.GetResponse())
                {
                    Stream dataStream = response.GetResponseStream();
                    StreamReader sr1 = new StreamReader(dataStream, Encoding.UTF8);
                    string str = sr1.ReadToEnd();
                    dataStream.Close();
                    sr1.Close();
                    //把Json数据反序列化到一个实体类中
                    list = JsonConvert.DeserializeObject>(str);
                }
            }
            catch (Exception e)
            {
                log.Warn(e);
                MessageBox.Show("获取失败!");
            }
        }
注意dll和其他命名空间的引用。

你可能感兴趣的:(C#,winform)