using DotNet4.Utilities;
using ryCommon;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;

namespace RyWeb
{
    /// <summary>
    /// 
    /// </summary>
   public class QuickWeb
    {
        /// <summary>
        /// 以post方式获取网页源码
        /// </summary>
        /// <param name="url"></param>
        /// <param name="post"></param>
        /// <param name="cookie"></param>
        /// <returns></returns>
        public HttpResult Post(string url, string post, string cookie)
        {
            return Post(url, post, "application/x-www-form-urlencoded", cookie);
        }
        /// <summary>
        /// 以post方式提交json内容
        /// </summary>
        /// <param name="url"></param>
        /// <param name="post"></param>
        /// <param name="cookie"></param>
        /// <returns></returns>
        public HttpResult PostJson(string url, string post, string cookie)
        {
            return Post(url, post, "application/json", cookie);
        }
        /// <summary>
        /// 以post方式提交json内容
        /// </summary>
        /// <param name="url"></param>
        /// <param name="post"></param>
        /// <returns></returns>
        public HttpResult PostJson(string url, string post)
        {
            return Post(url, post, "application/json", "");
        }
        /// <summary>
        /// 以post方式获取网页源码
        /// </summary>
        /// <param name="url"></param>
        /// <param name="post"></param>
        /// <param name="ContentType"></param>
        /// <param name="cookie"></param>
        /// <returns></returns>
        public HttpResult Post(string url, string post,string ContentType, string cookie)
        {
            try
            {
                HttpHelper t = new HttpHelper();
                HttpItem m = new HttpItem()
                {
                    URL = url,
                    Postdata = post,
                    ContentType = ContentType,
                    Method = "POST",
                    Timeout = Timeout,
                    ReadWriteTimeout = Timeout,
                    UserAgent = UserAgent,
                    Referer=Referer,
                    KeepAlive = false
                };
                if (cookie.Length > 0)
                {
                    m.Cookie = cookie;
                }
                HttpResult r = t.GetHtml(m);
                return r;
            }
            catch
            {
                HttpResult r = new HttpResult();
                return r;
            }
        }
        /// <summary>
        /// 以post方式获取网页源码
        /// </summary>
        /// <param name="url"></param>
        /// <param name="post"></param>
        /// <returns></returns>
        public HttpResult Post(string url, string post)
        {
            return Post(url, post, cookie);
        }
        /// <summary>
        /// 获取网址对应的文件大小
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public long GetSize(string url)
        {
            return GetSize(url, out _);
        }
        /// <summary>
        /// 获取网址对应的文件大小
        /// </summary>
        /// <param name="url"></param>
        /// <param name="errormsg"></param>
        /// <returns></returns>
        public long GetSize(string url,out string errormsg)
        {
            errormsg = "";
            try
            {
                HttpHelper t = new HttpHelper();
                HttpItem m = new HttpItem()
                {
                    URL = url,
                    Method = "HEAD",
                    Allowautoredirect = true,
                    Cookie = cookie,
                    Timeout = Timeout,
                    ReadWriteTimeout = Timeout,
                    UserAgent = UserAgent,
                    Referer = Referer,
                    KeepAlive=false
                };
                HttpResult r = t.GetHtml(m);
                if (r.StatusCode == System.Net.HttpStatusCode.OK)
                {
                    return GetContentLen();
                }
                else if (r.StatusCode == System.Net.HttpStatusCode.NotFound)
                {
                    errormsg = "404错误";
                    return -404;
                }
                else if (r.StatusCode == System.Net.HttpStatusCode.Found)
                {
                    return GetContentLen();
                }
                else if (r.StatusCode == 0)
                {
                    errormsg = r.Html;
                    return 0;
                }
                else if (r.StatusCode == System.Net.HttpStatusCode.MethodNotAllowed)
                {
                    System.Net.ServicePointManager.DefaultConnectionLimit = 512;
                    System.Net.HttpWebRequest request = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(url);
                    System.Net.HttpWebResponse response = (System.Net.HttpWebResponse)request.GetResponse();
                    long totalBytes = response.ContentLength;
                    return totalBytes;
                }
                else
                    return 0;
                long GetContentLen()
                {
                    var size = r.Header["Content-Length"].ToInt64(0);
                    if (size == 0)
                    {
                        System.Net.ServicePointManager.DefaultConnectionLimit = 512;
                        HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
                        request.AllowAutoRedirect = true;
                        request.Timeout = 3000;
                        request.ReadWriteTimeout = 3000;
                        HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                        long totalBytes = response.ContentLength;
                        return totalBytes;
                    }
                    else
                    {
                        return size;
                    }
                }
            }
            catch(Exception ex)
            {
                errormsg = ex.Message;
                return 0;
            }
        }
        /// <summary>
        /// 
        /// </summary>
        public string UserAgent
        {
            get; set;
        } = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)";
        /// <summary>
        /// 获取网页源码
        /// </summary>
        /// <param name="url"></param>
        /// <param name="cookie"></param>
        /// <returns></returns>
        public HttpResult Get(string url, string cookie)
        {
            try
            {
                HttpHelper t = new HttpHelper();
                HttpItem m = new HttpItem()
                {
                    URL = url,
                    ContentType= "application/x-www-form-urlencoded",
                    KeepAlive = false
                };
                if (cookie.Length>0)
                {
                    m.Cookie = cookie;
                }
                m.UserAgent = UserAgent;
                m.Allowautoredirect = true;
                m.Timeout = Timeout;
                m.ReadWriteTimeout = Timeout;
                m.Referer = Referer;
                HttpResult r = t.GetHtml(m);
                return r;
            }
            catch
            {
                HttpResult r = new HttpResult()
                {
                    Html = ""
                };
                return r;
            }
        }
        /// <summary>
        /// 获取网页源码
        /// </summary>
        /// <param name="url"></param>
        ///<param name="encoding"></param>
        /// <param name="cookie"></param>
        /// <returns></returns>
        public HttpResult Get(string url,Encoding encoding, string cookie)
        {
            try
            {
                HttpHelper t = new HttpHelper();
                HttpItem m = new HttpItem()
                {
                    URL = url,
                    KeepAlive = false
                };
                if (cookie != "")
                {
                    m.Cookie = cookie;
                }
                m.UserAgent = UserAgent;
                m.Encoding = encoding;
                m.Allowautoredirect = true;
                m.Timeout = Timeout;
                m.ReadWriteTimeout = Timeout;
                m.Referer = Referer;
                HttpResult r = t.GetHtml(m);
                return r;
            }
            catch
            {
                HttpResult r = new HttpResult()
                {
                    Html = ""
                };
                return r;
            }
        }
        /// <summary>
        /// 获取网页源码
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public HttpResult Get(string url)
        {
            return Get(url, cookie);
        }
        /// <summary>
        /// 
        /// </summary>
        public string Cookie
        {
            get { return cookie; }
        }
        /// <summary>
        /// 
        /// </summary>
        public string Referer
        {
            get;set;
        }
        /// <summary>
        /// 
        /// </summary>
        public int Timeout { get; set; } = 10000;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="str"></param>
        /// <param name="encode"></param>
        /// <returns></returns>
        public string UrlEncode(string str, Encoding encode)
        {
            StringBuilder sb = new StringBuilder();
            byte[] byStr = encode.GetBytes(str); //默认是System.Text.Encoding.Default.GetBytes(str)
            for (int i = 0; i < byStr.Length; i++)
            {
                var item = Convert.ToString(byStr[i], 16);
                if (item.Length == 1) { item = "0" + item; }
                sb.Append(@"%" + item);
            }
            return (sb.ToString());
        }
        private readonly string cookie = "";
        /// <summary>
        /// 将相对网址转换成绝对网址
        /// </summary>
        /// <param name="rel_url">相对网址</param>
        /// <param name="cur_pageUrl">当前页面地址</param>
        /// <returns>转换后的绝对网址</returns>
        public string ConvertUrl(string rel_url, string cur_pageUrl)
        {
            if (rel_url == "")
            {
                return cur_pageUrl;
            }
            try
            {
                string _rel_url = rel_url;
                if (_rel_url.IndexOf("//")==0)
                {
                    int iPos = cur_pageUrl.IndexOf(":");
                    if (iPos > 0)
                    {
                        _rel_url = cur_pageUrl.Substring(0,iPos)+ ":" + _rel_url;
                    }
                }
                Uri baseUri = new Uri(cur_pageUrl); // 
                Uri absoluteUri = new Uri(baseUri, _rel_url);//相对绝对路径都在这里转 这里的urlx ="../test.html"
                return absoluteUri.AbsoluteUri.Replace("&amp;", "&");// 
            }
            catch { return rel_url; }
        }
    }
}
