Ejemplo n.º 1
0
        public static SharpEntityResults <T> MakeRequestList <T>(string requestUrl, Type JSONResponseType) where T : SharpEntity
        //  requestUrl: Url completa del Web Service, amb l'opció sol·licitada
        //  JSONRensponseType:  tipus d'objecte que torna el Web Service (typeof(tipus))
        {
            try
            {
                HttpWebRequest request = WebRequest.Create(requestUrl) as HttpWebRequest; //WebRequest WR = WebRequest.Create(requestUrl);

                using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
                {
                    if (response.StatusCode != HttpStatusCode.OK)
                    {
                        throw new Exception(String.Format("Server error (HTTP {0}: {1}).", response.StatusCode, response.StatusDescription));
                    }

                    Stream                 stream1       = response.GetResponseStream();
                    StreamReader           sr            = new StreamReader(stream1);
                    string                 strsb         = sr.ReadToEnd();
                    SharpEntityResults <T> swapiResponse = JsonConvert.DeserializeObject <SharpEntityResults <T> >(strsb);
                    return(swapiResponse);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                return(null);
            }
        }
Ejemplo n.º 2
0
        public SharpEntityResults <Planet> GetAllPlanets(int pageNumber)
        {
            SharpEntityResults <Planet> result = null;

            try
            {
                result = MakeRequestList <Planet>(string.Format("{0}{1}{2}", apiUrl, "planets/", "?page=" + pageNumber), typeof(List <Planet>));
            }
            catch (NullReferenceException)
            {
            }
            return(result);
        }
Ejemplo n.º 3
0
        public SharpEntityResults <Film> GetAllFilms()
        {
            SharpEntityResults <Film> result = null;

            try
            {
                result = MakeRequestList <Film>(string.Format("{0}{1}", apiUrl, "films/"), typeof(List <Film>));
            }
            catch (NullReferenceException)
            {
            }
            return(result);
        }
Ejemplo n.º 4
0
        public SharpEntityResults <People> GetAllPeople(int pageNumber)
        {
            SharpEntityResults <People> result = MakeRequestList <People>(string.Format("{0}{1}{2}", apiUrl, "people/", "?page=" + pageNumber), typeof(List <People>));

            return(result);
        }