C# 如何接受服务器返回的Json数据?

2024-11-03 15:23:58
推荐回答(2个)
回答1:

public static string GetHTTPInfo(string urlPath, string eCode, int millisecond)
        {
            string str = null;
            HttpWebResponse response = null;
            StreamReader reader = null;
            try
            {
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(urlPath);
                request.Timeout = millisecond;
                response = (HttpWebResponse)request.GetResponse();
                reader = new StreamReader(response.GetResponseStream(), System.Text.Encoding.GetEncoding(eCode));
                str = reader.ReadToEnd();
            }
            catch
            {
                str = "";
            }
            finally
            {
                if (reader != null)
                {
                    reader.Close();
                    reader.Dispose();
                }
                if (response != null)
                {
                    response.Close();
                }
            }
            return str;
        }

回答2:

using Newtonsoft.Json;
string jsonstr = context.Request["jsonstr"];
JArray javascript = (JArray)JsonConvert.DeserializeObject(jsonstr);
用Newtonsoft.Json;