public Meta Load() { List<string> metaServerList = metaServerLocator.GetMetaServerList(); if (metaServerList == null || metaServerList.Count == 0) { throw new Exception("No meta server found."); } List<String> ipPorts = new List<String>(metaServerList); ipPorts.Shuffle(); foreach (string ipPort in ipPorts) { log.Debug(string.Format("Loading meta from server: {0}", ipPort)); try { string url = string.Format("http://{0}/meta", ipPort); if (metaCache.ReadFullFence() != null) { url += "?version=" + metaCache.ReadFullFence().Version; } HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url); req.Timeout = config.MetaServerConnectTimeoutInMills + config.MetaServerReadTimeoutInMills; using (HttpWebResponse res = (HttpWebResponse)req.BetterGetResponse()) { HttpStatusCode statusCode = res.StatusCode; if (statusCode == HttpStatusCode.OK) { using (var stream = new StreamReader(res.GetResponseStream(), Encoding.UTF8)) { string responseContent = stream.ReadToEnd(); JsonSerializerSettings settings = new JsonSerializerSettings(); settings.NullValueHandling = NullValueHandling.Ignore; metaCache.WriteFullFence(JsonConvert.DeserializeObject<Meta>(responseContent, settings)); return metaCache.ReadFullFence(); } } else if (statusCode == HttpStatusCode.NotModified) { return metaCache.ReadFullFence(); } } } catch (Exception ex) { log.Error(ex); } } throw new Exception(string.Format("Failed to load remote meta from {0}", ipPorts)); }
public static void fetchMeta() { string url = "http://meta.hermes.fws.qa.nt.ctripcorp.com/meta"; HttpWebRequest req = (HttpWebRequest)WebRequest.Create (url); req.Timeout = 5000; HttpWebResponse res = (HttpWebResponse)req.GetResponse (); HttpStatusCode statusCode = res.StatusCode; if (statusCode == HttpStatusCode.OK) { string responseContent = new StreamReader (res.GetResponseStream (), Encoding.UTF8).ReadToEnd (); JsonSerializerSettings settings = new JsonSerializerSettings(); settings.NullValueHandling = NullValueHandling.Ignore; Meta meta = JsonConvert.DeserializeObject<Meta> (responseContent, settings); Console.WriteLine (JsonConvert.SerializeObject (meta)); } else if (statusCode == HttpStatusCode.NotModified) { } }