示例#1
0
        private static Result GetJson(string url)
        {
            //HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
            //httpWebRequest.Method = WebRequestMethods.Http.Get;
            //httpWebRequest.Accept = "application/json";

            //var response = (HttpWebResponse)httpWebRequest.GetResponse();

            //StreamReader streamReader = new StreamReader(response.GetResponseStream());
            //String responseData = streamReader.ReadToEnd();

            //responseData = responseData.Replace("callback(", String.Empty);
            //responseData = responseData.Replace("})", "}");

            //return JsonConvert.DeserializeObject<RootObject>(responseData).Results[0];

            using (var webClient = new PersistantWebClient())
            {
                string responseData = webClient.DownloadString(url);
                responseData = responseData.Replace("callback(", String.Empty);
                responseData = responseData.Replace("})", "}");
                RootObject json = JsonConvert.DeserializeObject <RootObject>(responseData);
                return(json.Results[0]);
            }
        }
示例#2
0
        internal static string GetJsonString(string url)
        {
            string json = null;

            using (var webClient = new PersistantWebClient())
            {
                try
                {
                    json = webClient.DownloadString(url);
                }
                catch (WebException error)
                {
                    // 404 is expected, indicates that no results were found.
                    if (error.Status != WebExceptionStatus.ProtocolError)
                    {
                        throw;
                    }
                }
            }

            return(json);
        }