コード例 #1
0
ファイル: Cache.cs プロジェクト: riskypathak/WebSpider
        /// <summary>
        /// Add URL to cache
        /// </summary>
        /// <param name="Url"></param>
        /// <returns></returns>
        //private static HtmlAgilityPack.HtmlDocument AddUrl(String Url)
        //{
        //    if (!IsCachedUrl(Url))
        //    {
        //        Crawler crawler = new Crawler();
        //        crawler.Url = Url;
        //        HtmlAgilityPack.HtmlDocument document = crawler.GetDocument();
        //        String FileName = GenerateFileName();
        //        document.Save(GetFullFileName(FileName));
        //        DataRow dRow = CacheDB.Tables[_dtName].NewRow();
        //        dRow["Url"] = Url;
        //        dRow["FileName"] = FileName;
        //        dRow["LastUpdated"] = DateTime.Now;
        //        dRow["ValidTill"] = DateTime.Now.AddMinutes(_cacheValidity);
        //        CacheDB.Tables[_dtName].Rows.Add(dRow);
        //        SaveCache();
        //        return document;
        //    }
        //    return new HtmlAgilityPack.HtmlDocument();
        //}
        public byte[] AddUrl(String Url, NameValueCollection formData = null )
        {
            Crawler crawler = new Crawler();
            byte[] responseBytes;
            crawler.Url = Url;
            WebClient webClient = new WebClient();
            if (!ReferenceEquals(formData, null))
            {
                webClient.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
                responseBytes = webClient.UploadValues(Url, "POST", formData);
            }
            else
            {
                responseBytes = webClient.DownloadData(Url);
            }
            string resultAuthTicket = Encoding.UTF8.GetString(responseBytes);
            webClient.Dispose();
            //String FileName = GenerateFileName();
            //File.WriteAllBytes(FileName, responseBytes);
            //DataRow dRow = CacheDB.Tables[_dtName].NewRow();
            //dRow["Url"] = Url;
            //dRow["FileName"] = FileName;
            //dRow["LastUpdated"] = DateTime.Now;
            //dRow["ValidTill"] = DateTime.Now.AddMinutes(_cacheValidity);
            //CacheDB.Tables[_dtName].Rows.Add(dRow);
            //SaveCache();
            SaveCache(Url, responseBytes);

            return responseBytes;
        }
コード例 #2
0
ファイル: Cache.cs プロジェクト: riskypathak/WebSpider
 public static HtmlAgilityPack.HtmlDocument PostUrl(String Url, WebHeaderCollection Header, NameValueCollection formData)
 {
     Crawler crawler = new Crawler();
     byte[] responseBytes;
     crawler.Url = Url;
     WebClient webClient = new WebClient();
     if (!ReferenceEquals(Header, null))
     {
         webClient.Headers = Header;
     }
     if (!ReferenceEquals(formData, null))
     {
         webClient.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
         responseBytes = webClient.UploadValues(Url, "POST", formData);
     }
     else
     {
         responseBytes = webClient.DownloadData(Url);
     }
     string resultAuthTicket = Encoding.UTF8.GetString(responseBytes);
     webClient.Dispose();
     MemoryStream mStream = new MemoryStream(responseBytes);
     HtmlAgilityPack.HtmlDocument document = new HtmlAgilityPack.HtmlDocument();
     document.Load(mStream);
     return document;
 }
コード例 #3
0
ファイル: Browser.cs プロジェクト: riskypathak/WebSpider
        public HtmlDocument PostRequest(String Url, WebHeaderCollection Header, NameValueCollection formData)
        {
            for (int RetryCount = 1 ; true ; RetryCount++)
            {
                try
                {
                    HttpClient httpClient = new HttpClient();

                    Crawler crawler = new Crawler();
                    byte[] responseBytes;
                    crawler.Url = Url;
                    //WebClient httpClient = new WebClient();
                    if (!ReferenceEquals(Header, null))
                    {
                        httpClient.Headers = Header;
                    }
                    if (!ReferenceEquals(formData, null))
                    {
                        httpClient.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
                        responseBytes = httpClient.UploadValues(Url, "POST", formData);
                    }
                    else
                    {
                        responseBytes = httpClient.DownloadData(Url);
                    }
                    string resultAuthTicket = Encoding.UTF8.GetString(responseBytes);
                    httpClient.Dispose();
                    MemoryStream mStream = new MemoryStream(responseBytes);
                    HtmlAgilityPack.HtmlDocument document = new HtmlAgilityPack.HtmlDocument();
                    document.Load(mStream);
                    return document;
                }
                catch (Exception ex)
                {
                    if (RetryCount == MaxRetryCount)
                        throw ex;
                }
            }
        }