Exemple #1
0
        /// <summary>
        /// 获取post过来数据
        /// </summary>
        /// <returns></returns>
        //public string ResponseMsg()
        //{
        //    Stream postStr = Request.InputStream;
        //    StreamReader sr = new StreamReader(postStr, Encoding.UTF8);
        //    string content = sr.ReadToEnd();
        //    return content;
        //}
        /// <summary>
        /// 通过url获取json格式数据
        /// </summary>
        /// <param name="url"></param>
        /// <param name="method"></param>
        /// <returns></returns>
        internal static string RequestMsg(string url, string method, string data = null, bool requesterHeader = false)
        {
            HttpWebRequest req = WebRequest.Create(url) as HttpWebRequest;

            if (requesterHeader)
            {
                req.Headers.Add(GlobalContext.RequestHeader, GlobalContext.CreateApiRequestHeader());
            }
            Stream reqStream = null;

            if (method == "POST")
            {
                byte[] byData = Encoding.Default.GetBytes(data);
                req.ContentLength = byData.Length;
                reqStream         = req.GetRequestStream();
                reqStream.Write(byData, 0, byData.Length);
            }

            req.ContentType = "application/json";
            req.Method      = method;
            HttpWebResponse res      = req.GetResponse() as HttpWebResponse;
            Stream          inStream = res.GetResponseStream();
            StreamReader    sr       = new StreamReader(inStream, Encoding.Default);
            string          content  = sr.ReadToEnd();

            sr.Close();
            inStream.Close();
            res.Close();
            if (null != reqStream)
            {
                reqStream.Close();
            }
            return(content);
        }
Exemple #2
0
        public static HttpContent HttpClientGetContentAsync(string url, bool requesterHeader = false)
        {
            var client = CreateHttpClient();

            if (requesterHeader)
            {
                client.DefaultRequestHeaders.Add(GlobalContext.RequestHeader, GlobalContext.CreateApiRequestHeader());
            }
            var result = client.GetAsync(url).Result;

            return(result.Content);
        }
        public static dynamic HttpClientGetAsync(string url, bool requesterHeader = false)
        {
            var client = new HttpClient();

            if (requesterHeader)
            {
                client.DefaultRequestHeaders.Add(GlobalContext.RequestHeader, GlobalContext.CreateApiRequestHeader());
            }
            var result = client.GetAsync(url).Result;

            if (!result.IsSuccessStatusCode)
            {
                return(string.Empty);
            }
            return(DynamicJson.Parse(result.Content.ReadAsStringAsync().Result));
        }