예제 #1
0
        public static void TestUnPaywall()
        {
            IHttpRequest httpRequest = new HttpRequest();
            //string strValue = "10.1038/nature12373";
            string strValue = "test";

            string searchURL = "https://api.unpaywall.org/v2/{0}?email=YOUR_EMAIL";

            try
            {
                IResopnse resopnse = httpRequest.Get(string.Format(searchURL, strValue));



                JObject JsonData = Newtonsoft.Json.JsonConvert.DeserializeObject <Newtonsoft.Json.Linq.JObject>(resopnse.Result);

                ResultDetail resultDetail = new ResultDetail();

                resultDetail.Title           = JsonData["title"].ToString();
                resultDetail.PublicationDate = JsonData["published_date"].ToString();
                resultDetail.PublicationName = JsonData["publisher"].ToString();


                string Authors = string.Empty;

                if (JsonData["z_authors"] != null)
                {
                    for (int i = 0; i < JsonData["z_authors"].Count(); i++)
                    {
                        if (Authors.Length > 0)
                        {
                            Authors += ";";
                        }

                        Authors += JsonData["z_authors"][i]["family"].ToString();
                    }
                }
                resultDetail.Authors = Authors;

                if (JsonData["is_oa"].ToString() == "True")
                {
                    if (JsonData["best_oa_location"] != null)
                    {
                        if (JsonData["best_oa_location"]["url"] != null)
                        {
                            resultDetail.URI = JsonData["best_oa_location"]["url"].ToString();
                        }
                        else if (JsonData["best_oa_location"]["url_for_pdf"] != null)
                        {
                            resultDetail.URI = JsonData["best_oa_location"]["url_for_pdf"].ToString();
                        }
                    }
                }

                System.Console.WriteLine(string.Format("Title:{0};Authors:{1};URI:{2};publisher:{3};published_date{4}"
                                                       , resultDetail.Title, resultDetail.Authors, resultDetail.URI, resultDetail.PublicationName, resultDetail.PublicationDate));

                //if (resopnse.StatusCode == System.Net.HttpStatusCode.NotFound)
                //{

                //   System.Console.WriteLine(resopnse.Result);
                //}
                //else
                //{
                //   System.Console.WriteLine(resopnse.Result);

                //}
            }
            catch (Exception ex)
            {
                throw;
            }
        }
예제 #2
0
        /// <summary>
        /// if error  throw ex
        /// </summary>
        /// <param name="URL"></param>
        /// <param name="ActionStep">action step</param>
        /// <returns></returns>
        private static ResponseModel RequestURL_GET(string URL, string ActionStep)
        {
            int action = 3;

            try
            {
                if (action == 1)
                {
                    var client = new RestClient(URL);
                    client.Timeout = -1;
                    var           request  = new RestRequest(Method.GET);
                    IRestResponse response = client.Execute(request);

                    if (response.StatusCode == HttpStatusCode.OK ||
                        response.StatusCode == HttpStatusCode.Created ||
                        response.StatusCode == HttpStatusCode.Accepted)
                    {
                        XmlDocument xmlDoc = new XmlDocument();
                        xmlDoc.Load(response.Content);
                        var responseData = new ResponseModel()
                        {
                            XmlDoc     = xmlDoc,
                            StatusCode = response.StatusCode,
                        };
                        return(responseData);
                    }
                    else
                    {
                        var responseData = new ResponseModel()
                        {
                            XmlDoc     = null,
                            StatusCode = response.StatusCode,
                        };

                        return(responseData);
                    }
                }
                else if (action == 2)
                {
                    HttpRequest httpRequest = new HttpRequest();
                    IResopnse   response    = httpRequest.Get(URL);

                    if (response.StatusCode == HttpStatusCode.OK ||
                        response.StatusCode == HttpStatusCode.Created ||
                        response.StatusCode == HttpStatusCode.Accepted)
                    {
                        XmlDocument xmlDoc = new XmlDocument();
                        xmlDoc.Load(response.Result);
                        var responseData = new ResponseModel()
                        {
                            XmlDoc     = xmlDoc,
                            StatusCode = response.StatusCode,
                        };
                        return(responseData);
                    }
                    else
                    {
                        var responseData = new ResponseModel()
                        {
                            XmlDoc     = null,
                            StatusCode = response.StatusCode,
                        };

                        return(responseData);
                    }
                }
                else
                {
                    try
                    {
                        HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(URL);
                        httpWebRequest.Method          = "GET";
                        httpWebRequest.Timeout         = -1;
                        httpWebRequest.KeepAlive       = true;
                        httpWebRequest.ProtocolVersion = HttpVersion.Version10;
                        httpWebRequest.ServicePoint.ConnectionLimit = 1;
                        using (HttpWebResponse response = (HttpWebResponse)httpWebRequest.GetResponse())
                        {
                            if (response.StatusCode == HttpStatusCode.OK ||
                                response.StatusCode == HttpStatusCode.Created ||
                                response.StatusCode == HttpStatusCode.Accepted)
                            {
                                using (Stream s = response.GetResponseStream())
                                {
                                    using (StreamReader sr = new StreamReader(s, System.Text.Encoding.Default))
                                    {
                                        XmlDocument xmlDoc = new XmlDocument();
                                        xmlDoc.Load(sr);
                                        var responseData = new ResponseModel()
                                        {
                                            XmlDoc     = xmlDoc,
                                            StatusCode = response.StatusCode,
                                        };
                                        return(responseData);
                                    }
                                }
                            }
                            else
                            {
                                var responseData = new ResponseModel()
                                {
                                    XmlDoc     = null,
                                    StatusCode = response.StatusCode,
                                };

                                return(responseData);
                            }
                        }
                    }
                    catch (WebException ex)
                    {
                        if (ex.Response != null)
                        {
                            HttpWebResponse response = (HttpWebResponse)ex.Response;
                            if (response != null)
                            {
                                using (var stream = response.GetResponseStream())
                                {
                                    using (var streamReader = new StreamReader(stream, Encoding.Default))
                                    {
                                        throw new Exception(string.Format("step:{3},StatusCode:{0}-{1},Result:{2}"
                                                                          , (int)response.StatusCode, response.StatusCode.ToString(), streamReader.ReadToEnd(), ActionStep));
                                    }
                                }
                            }
                        }


                        throw new Exception(string.Format("step:{0},message:{1}", ActionStep, ex.Message));
                    }
                    catch (Exception ex)
                    {
                        throw new Exception(string.Format("step:{0},message:{1}", ActionStep, ex.Message));
                    }
                }
            }
            catch (Exception)
            {
                throw;
            }

            return(null);
        }