using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Security;
using System.Runtime.InteropServices;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Web;
namespace Helper
{
    /// <summary>
    /// 网络编程帮助类,代码全是抄的
    /// </summary>
    [ComVisible(true)]
    public static class HttpHelper
    {
        public static int TimeOut { get; set; }
        public static string Proxy { get; set; }
        public static bool UseProxy { get; set; }
        [DllImport("wininet.dll")]
        public extern static bool InternetGetConnectedState(out int Description, int ReservedValue);
        ///<summary>
        /// 查看网络是否连接到公网
        ///</summary>
        ///<returns>返回Ture：可以连接到Internet，False则连接不上</returns>
        public static bool IsConnectedToInternet
        {
            get
            {
                int Desc;
                return InternetGetConnectedState(out Desc, 0);
            }
        }
        public static void SetSSL()
        {
            try
            {
                ServicePointManager.SecurityProtocol = (SecurityProtocolType)192 | (SecurityProtocolType)768 | (SecurityProtocolType)3072;
            }
            catch (Exception)
            { }
            try
            {
                ServicePointManager.ServerCertificateValidationCallback = ValidateServerCertificate;
            }
            catch (Exception)
            { }
        }
        /// <summary>
        /// 解决Error"基础连接已经关闭: 未能为SSL/TLS 安全通道建立信任关系。"
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="certificate"></param>
        /// <param name="chain"></param>
        /// <param name="sslPolicyErrors"></param>
        /// <returns></returns>
        public static bool ValidateServerCertificate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
        {
            return true;
        }
        class HttpClient : WebClient
        {
            public void GetFile(string path, string url)
            {
                if (url == string.Empty)
                { throw new WebException("下载地址为空"); }
                base.Headers["User-Agent"] = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36 OPR/48.0.2685.52";
                DownloadFile(url, path);
            }
        }
        /// <summary>
        /// get请求
        /// </summary>
        /// <param name="urlstr">url</param>
        /// <returns></returns>
        public static string Get(string url, bool _throw = false)
        {
            SetSSL();
            string message = string.Empty;
            string[] urls = { url.Replace("https://", "http://"), url.Replace("http://", "https://") };
            foreach (string item in urls)
            {
                try
                {
                    HttpWebRequest req = (HttpWebRequest)WebRequest.Create(item);
                    req.Accept = "text/html, application/xhtml+xml, */*";
                    req.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)";
                    req.KeepAlive = true;
                    req.Timeout = TimeOut == 0 ? 10000 : TimeOut;
                    req.ProtocolVersion = HttpVersion.Version11;
                    ServicePointManager.Expect100Continue = false;
                    HttpWebResponse response = (HttpWebResponse)req.GetResponse();
                    Stream responseStream = response.GetResponseStream();
                    StreamReader reader = new StreamReader(responseStream, Encoding.UTF8);
                    message = reader.ReadToEnd();
                    reader.Close();
                    responseStream.Close();
                    break;
                }
                catch (Exception ex)
                {
                    message = ex.Message;
                    if (_throw)
                        throw;
                }
            }
            return ConvertEx.UnicodeToString(message);
        }
        /// <summary>
        /// get请求
        /// </summary>
        /// <param name="url">url</param>
        /// <returns></returns>
        public static string Get(string url, Encoding en, bool _throw = false)
        {
            SetSSL(); 
            string message = string.Empty;
            string[] urls = { url.Replace("https://", "http://"), url.Replace("http://", "https://") };
            foreach (string item in urls)
            {
                try
                {
                    HttpWebRequest req = (HttpWebRequest)WebRequest.Create(item);
                    req.Accept = "text/html, application/xhtml+xml, */*";
                    req.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)";
                    req.KeepAlive = true;
                    req.Timeout = TimeOut == 0 ? 10000 : TimeOut;
                    req.ProtocolVersion = HttpVersion.Version11;
                    ServicePointManager.Expect100Continue = false;
                    HttpWebResponse response = (HttpWebResponse)req.GetResponse();
                    Stream responseStream = response.GetResponseStream();
                    StreamReader reader = new StreamReader(responseStream, en);
                    message = reader.ReadToEnd();
                    reader.Close();
                    responseStream.Close();
                    break;
                }
                catch (Exception ex)
                {
                    message = ex.Message;
                    if (_throw)
                        throw;
                }
            }
            return ConvertEx.UnicodeToString(message);
        }
        /// <summary>
        /// 下载文件
        /// </summary>
        /// <param name="path">目录</param>
        /// <param name="url">下载地址</param>
        public static void Down(string path, string url)
        {
            SetSSL();
            HttpClient Client = new HttpClient();
            try
            { Client.GetFile(path.Replace("\\\\", "\\"), url); }
            catch (Exception err)
            { throw new Exception("void Down(string,string)发生了异常:" + err.Message); }
        }
        public static string ConvereUrl(string url)
        {
            SetSSL();
            try
            {
                HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
                req.Method = "HEAD";
                req.AllowAutoRedirect = false;
                req.Timeout = TimeOut == 0 ? 10000 : TimeOut;
                HttpWebResponse myResp = (HttpWebResponse)req.GetResponse();
                if (myResp.StatusCode == HttpStatusCode.Redirect)
                { return myResp.GetResponseHeader("Location"); }
            }
            catch (Exception)
            { }
            return url;
        }

        /// <summary>  
        /// POST请求与获取结果  
        /// </summary>  
        public static string Post(string Url, string postDataStr)
        {
            SetSSL();
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(Url);
            req.Method = "POST";
            req.Timeout = TimeOut == 0 ? 10000 : TimeOut;
            req.ContentType = "application/x-www-form-urlencoded";
            req.ContentLength = postDataStr.Length;
            StreamWriter writer = new StreamWriter(req.GetRequestStream(), Encoding.ASCII);
            writer.Write(postDataStr);
            writer.Flush();
            HttpWebResponse response = (HttpWebResponse)req.GetResponse();
            string encoding = response.ContentEncoding;
            if (encoding == null || encoding.Length < 1)
            {
                encoding = "UTF-8"; //默认编码  
            }
            StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding(encoding));
            string retString = reader.ReadToEnd();
            return retString;
        }

        /// <summary>
        /// 检查url链接是否有效
        /// </summary>
        /// <param name="strUri"></param>
        /// <returns></returns>
        public static bool CheckUri(string url)
        {
            SetSSL();
            try
            {
                if (url.Contains("http"))
                {
                    HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
                    req.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko";
                    req.Method = "GET";
                    req.Timeout = TimeOut == 0 ? 10000 : TimeOut;
                    HttpWebResponse myHttpWebResponse = (HttpWebResponse)req.GetResponse();
                    if (myHttpWebResponse.StatusCode == HttpStatusCode.OK)
                    {
                        myHttpWebResponse.Close();
                        return true;
                    }
                }
                else { return File.Exists(url); }
                return false;
            }
            catch (Exception)
            {
                return false;
            }
        }
    }
}
