Ejemplo n.º 1
0
        public static string RequestGet(RestInfo restInfo)
        {
            string result = "";

            if (restInfo == null)
            {
                return(result);
            }

            string path = string.Format("{0}?{1}", restInfo.Path, restInfo.GetQuery());

            if (string.IsNullOrEmpty(restInfo.Param))
            {
                path = string.Format("{0}", restInfo.Path);
            }

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(restInfo.Path);

            request.Method      = "GET";
            request.Accept      = "*/*";
            request.ContentType = "application/x-www-form-urlencoded";

            // response
            WebResponse  response     = request.GetResponse();
            StreamReader streamReader = new StreamReader(response.GetResponseStream());

            return(result);
        }
Ejemplo n.º 2
0
        private async Task <string> RequestGetAsync(RestInfo restInfo)
        {
            string path = string.Format("{0}?{1}", restInfo.Path, restInfo.GetQuery());

            if (string.IsNullOrEmpty(restInfo.Param))
            {
                path = string.Format("{0}", restInfo.Path);
            }

            var response = await _httpClient.GetStringAsync(path);

            //JContainer jsonResult = JObject.Parse(response);
            return(response);
        }
Ejemplo n.º 3
0
        public static string RequestPost1(RestInfo restInfo)
        {
            string result = "";

            if (restInfo == null)
            {
                return(result);
            }

            string         path    = string.Format("{0}", restInfo.Path);
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(restInfo.Path);

            //CookieContainer cookieContainer = new CookieContainer();
            //request.CookieContainer = cookieContainer;
            request.UserAgent   = _userAgent;
            request.Method      = "POST";
            request.Accept      = "*/*";
            request.ContentType = "application/x-www-form-urlencoded";

            StreamWriter requestWriter = new StreamWriter(request.GetRequestStream());

            requestWriter.Write(restInfo.GetQuery());
            requestWriter.Close();

            // response
            try
            {
                HttpWebResponse response     = (HttpWebResponse)request.GetResponse();
                StreamReader    streamReader = new StreamReader(response.GetResponseStream());
                result = streamReader.ReadToEnd();
            }
            catch (WebException ex)
            {
                using (var stream = ex.Response.GetResponseStream())
                    using (var reader = new StreamReader(stream))
                    {
                        result = reader.ReadToEnd();
                    }
            }  catch (Exception e)
            {
                result = "network error";
            }

            return(result);
        }