using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text;

namespace PushPlatform.Common.Util
{
    public class RequestHelp
    {
        private static object _Locker = new object();
        #region 返回JSon数据
        /// <summary>  
        /// 返回JSon数据  
        /// </summary>  
        /// <param name="JSONData">要处理的JSON数据</param>  
        /// <param name="Url">要提交的URL</param>  
        /// <returns>返回的JSON处理字符串</returns>  
        public static string GetResponseData(string JSONData, string Url)
        {
            byte[] bytes = Encoding.UTF8.GetBytes(JSONData);
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
            request.Method = "POST";
            request.ContentLength = bytes.Length;
            request.ContentType = "json";
            Stream reqstream = request.GetRequestStream();
            reqstream.Write(bytes, 0, bytes.Length);

            //声明一个HttpWebRequest请求  
            request.Timeout = 90000;
            //设置连接超时时间  
            request.Headers.Set("Pragma", "no-cache");
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            Stream streamReceive = response.GetResponseStream();
            Encoding encoding = Encoding.UTF8;

            StreamReader streamReader = new StreamReader(streamReceive, encoding);
            string strResult = streamReader.ReadToEnd();
            streamReceive.Dispose();
            streamReader.Dispose();
            return strResult;
        }
        #endregion

        #region 创建访问GET
        /// <summary>
        /// 创建访问
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public static string GetResponseData(string url, int requestCount = 1)
        {
            string strResult = "";
            try
            {
                lock (_Locker)
                {
                    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
                    request.Method = "GET";
                    request.Timeout = 90000;
                    //设置连接超时时间  
                    request.Headers.Set("Pragma", "no-cache");
                    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                    Stream streamReceive = response.GetResponseStream();
                    Encoding encoding = Encoding.UTF8;
                    StreamReader streamReader = new StreamReader(streamReceive,encoding);
                    strResult = streamReader.ReadToEnd();
                    streamReceive.Dispose();
                    streamReader.Dispose();
                }
            }
            catch (Exception ex)
            {
                if (requestCount > 2)
                    return strResult;
                strResult = GetResponseData(url, ++requestCount);
                LogHelper.Error(ex);
            }
            return strResult;
        }
        #endregion

        #region 根据接口地址获得接口返回信息
        /// <summary>
        /// 
        /// </summary>
        /// <param name="url">接口地址</param>
        /// <returns></returns>
        public static string GetPostResponse(string url)
        {
            //创建请求
            HttpWebRequest myreq = (HttpWebRequest)HttpWebRequest.Create(url);
            myreq.Method = "POST";
            myreq.Timeout = 7200;
            //得到相应对象
            HttpWebResponse httpWResp = (HttpWebResponse)myreq.GetResponse();
            //获取流
            Stream myStream = httpWResp.GetResponseStream();
            StreamReader sr = new StreamReader(myStream, System.Text.Encoding.UTF8);
            System.Text.StringBuilder strBuilder = new System.Text.StringBuilder();
            while (-1 != sr.Peek())
            {
                strBuilder.Append(sr.ReadLine());
            }
            return strBuilder.ToString();
        }
        #endregion
    }
}
