Exemplo n.º 1
0
        private byte[] Get(string url, Dictionary <string, object> values)
        {
            if (values.Count > 0)
            {
                List <String> items = new List <String>();

                foreach (String name in values.Keys)
                {
                    items.Add(String.Concat(name, "=", System.Web.HttpUtility.UrlEncode((values[name] ?? string.Empty).ToString())));
                }

                string queryString = String.Join("&", items.ToArray());

                url += "?" + queryString;
            }
            byte[] response = null;
            using (WebClient client = new WebClientWithTimeout(this.Timeout, this.KeepAlive))
            {
                if (!string.IsNullOrWhiteSpace(Header))
                {
                    var headers = Header.Split('\n');
                    foreach (var header in headers)
                    {
                        if (!string.IsNullOrWhiteSpace(header))
                        {
                            client.Headers.Add(header);
                        }
                    }
                }
                response = client.DownloadData(url);
            }
            return(response);
        }
Exemplo n.º 2
0
        /// <summary>
        /// 获取Page数
        /// </summary>
        /// <returns></returns>
        public int GetPageCount(List <string> tags = null)
        {
            int pageCount = 1;
            var url       = site + post;

            //获取最大page数
            url += Page(pageCount);
            //获取最大page数
            url += Page(pageCount);
            if (tags != null)
            {
                url += Tags(tags);
            }
            if (isnsfw == false)
            {
                url += " rating:safe";
            }
            using (WebClientWithTimeout webclient = new WebClientWithTimeout())
            {
                //伪装成Chrome
                webclient.Headers.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36");
                bool   isTimeout = false;
                string result    = "";
                do
                {
                    try
                    {
                        result    = Encoding.GetEncoding("UTF-8").GetString(webclient.DownloadData(url.Replace(".json", "")));
                        isTimeout = false;
                    }
                    catch (WebException we)
                    {
                        switch (we.Status)
                        {
                        case WebExceptionStatus.Timeout:
                            isTimeout = true;
                            break;

                        default:
                            throw new Exception(we.Message);
                        }
                    }
                } while (isTimeout);

                string         patten   = @"(href|rel|title)=""([^""]*)";
                List <Element> elements = new List <Element>();
                Regex.Matches(result, patten).Cast <Match>().ToList().ForEach(x =>
                {
                    if (x.Groups[1].Value == "href")
                    {
                        elements.Add(new Element {
                            href = x.Groups[2].Value
                        });                                                     //添加到元素中
                    }
                    if (x.Groups[1].Value == "title")
                    {
                        elements[elements.Count - 1].title = x.Groups[2].Value;
                    }
                });
                Element lastPageElement = elements.Find(x => x.title == "Last Page");
                //别问我这是什么逻辑,当时就是这样翻源码的QwQ
                //能用就好(有时间会改改
                if (lastPageElement != null)
                {
                    int i = lastPageElement.href.IndexOf("="); //找=的位置
                    int j = lastPageElement.href.IndexOf("&"); //找&的位置
                    pageCount = Convert.ToInt32(lastPageElement.href.Substring(i + 1).Substring(0, j - i - 1));
                    //Console.WriteLine("共找到{0}页内容,正在解析...", pageCount);
                }
            }
            return(pageCount);
        }