Ejemplo n.º 1
0
        private string DoRequest(HttpWebRequest wreq, AlchemyAPI_BaseParams.OutputMode outputMode)
        {
            using (HttpWebResponse wres = wreq.GetResponse() as HttpWebResponse)
            {
                StreamReader r = new StreamReader(wres.GetResponseStream());

                string xml = r.ReadToEnd();

                if (string.IsNullOrEmpty(xml))
                {
                    throw new XmlException("The API request returned back an empty response. Please verify that the url is correct.");
                }

                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.LoadXml(xml);

                XmlElement root = xmlDoc.DocumentElement;

                if (AlchemyAPI_BaseParams.OutputMode.XML == outputMode)
                {
                    XmlNode status = root.SelectSingleNode("/results/status");

                    if (status.InnerText != "OK")
                    {
                        string errorMessage = "Error making API call.";

                        try
                        {
                            XmlNode statusInfo = root.SelectSingleNode("/results/statusInfo");
                            errorMessage = statusInfo.InnerText;
                        }
                        catch
                        {
                            // some problem with the statusInfo.  Return the generic message.
                        }

                        System.ApplicationException ex = new System.ApplicationException(errorMessage);

                        throw ex;
                    }
                }
                else if (AlchemyAPI_BaseParams.OutputMode.RDF == outputMode)
                {
                    XmlNamespaceManager nm = new XmlNamespaceManager(xmlDoc.NameTable);
                    nm.AddNamespace("rdf", "http://www.w3.org/1999/02/22-rdf-syntax-ns#");
                    nm.AddNamespace("aapi", "http://rdf.alchemyapi.com/rdf/v1/s/aapi-schema#");
                    XmlNode status = root.SelectSingleNode("/rdf:RDF/rdf:Description/aapi:ResultStatus", nm);

                    if (status.InnerText != "OK")
                    {
                        System.ApplicationException ex =
                            new System.ApplicationException("Error making API call.");

                        throw ex;
                    }
                }

                return(xml);
            }
        }
Ejemplo n.º 2
0
        private string DoRequest(HttpWebRequest wreq, AlchemyAPI_BaseParams.OutputMode outputMode)
        {
            using (var wres = wreq.GetResponse() as HttpWebResponse)
            {
                var r = new StreamReader(wres.GetResponseStream());

                var json = r.ReadToEnd();

                return(json);
            }
        }
Ejemplo n.º 3
0
        private string DoRequest(HttpWebRequest wreq, AlchemyAPI_BaseParams.OutputMode outputMode)
        {
            using (HttpWebResponse wres = wreq.GetResponse() as HttpWebResponse)
            {
                StreamReader r = new StreamReader(wres.GetResponseStream());

                string xml = r.ReadToEnd();

                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.LoadXml(xml);

                XmlElement root = xmlDoc.DocumentElement;

                if (AlchemyAPI_BaseParams.OutputMode.XML == outputMode)
                {
                    XmlNode status = root.SelectSingleNode("/results/status");

                    if (status.InnerText != "OK")
                    {
                        System.ApplicationException ex =
                            new System.ApplicationException("Error making API call.");

                        throw ex;
                    }
                }
                else if (AlchemyAPI_BaseParams.OutputMode.RDF == outputMode)
                {
                    XmlNamespaceManager nm = new XmlNamespaceManager(xmlDoc.NameTable);
                    nm.AddNamespace("rdf", "http://www.w3.org/1999/02/22-rdf-syntax-ns#");
                    nm.AddNamespace("aapi", "http://rdf.alchemyapi.com/rdf/v1/s/aapi-schema#");
                    XmlNode status = root.SelectSingleNode("/rdf:RDF/rdf:Description/aapi:ResultStatus", nm);

                    if (status.InnerText != "OK")
                    {
                        System.ApplicationException ex =
                            new System.ApplicationException("Error making API call.");

                        throw ex;
                    }
                }

                return(xml);
            }
        }