using System;
using System.IO;
using System.Net;
using System.Text;
using ChuPiao.Common.Utils;

namespace ChuPiao.Common.Core.Net
{
    public class HttpUtil
    {
        /// <summary>
        /// POST数据到某个页面，并取回返回值
        /// </summary>
        /// <param name="url"></param>
        /// <param name="postData"></param>
        /// <param name="contentType">一般用"application/x-www-form-urlencoded"</param>
        /// <param name="encoding"></param>
        /// <param name="timeout"></param>
        /// <returns></returns>
        public static string Post(string url, string postData, string contentType, Encoding encoding, int timeout)
        {
            Stream outstream = null;
            Stream instream = null;
            StreamReader sr = null;
            HttpWebResponse response = null;
            HttpWebRequest request = null;
            byte[] data = encoding.GetBytes(postData);
            // 准备请求... 
            try
            {
                // 设置参数 

                request = WebRequest.Create(url) as HttpWebRequest;
                CookieContainer cookieContainer = new CookieContainer();
                request.CookieContainer = cookieContainer;
                request.AllowAutoRedirect = true;
                request.Method = "POST";
                request.ContentType = contentType;
                //request.ContentType = "application/x-www-form-urlencoded";
                request.ContentLength = data.Length;
                request.Headers.Add("charset", "gbk");
                request.Timeout = timeout;
                outstream = request.GetRequestStream();
                outstream.Write(data, 0, data.Length);
                //outstream.Close();
                //发送请求并获取相应回应数据 
                response = request.GetResponse() as HttpWebResponse;
                //直到request.GetResponse()程序才开始向目标网页发送Post请求 
                instream = response.GetResponseStream();
                sr = new StreamReader(instream, encoding);
                //返回结果网页（html）代码 
                string content = sr.ReadToEnd();
                return content;
            }
            catch (Exception ex)
            {
                throw ex;
                return string.Empty;
            }
            finally
            {
                if (request != null)
                {
                    request.Abort();
                    request = null;
                }
                if (response != null)
                {
                    response.Close();
                    response = null;
                }
                if (outstream != null)
                {
                    outstream.Close();
                    outstream = null;
                }
                if (instream != null)
                {
                    instream.Close();
                    instream = null;
                }
                if (sr != null)
                {
                    sr.Close();
                    sr = null;
                }
            }
        }


        public static bool TestConnection(string url)
        {
            HttpWebRequest request = null;
            WebResponse response = null;
            try
            {
                request = WebRequest.Create(url) as HttpWebRequest;
                request.AllowAutoRedirect = true;
                request.Method = "GET";
                request.ContentType = "application/x-www-form-urlencoded";
                request.Timeout = 3000;

                response = request.GetResponse();
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
            finally
            {
                if (request != null)
                {
                    request.Abort();
                    request = null;
                }
                if (response != null)
                {
                    response.Close();
                    response = null;
                }
            }
        }
    }
}
