예제 #1
0
        public String AjaxPost(String Url, String JsonStringParams)
        {
            for (int RetryCount = 1 ; true ; RetryCount++)
            {
                try
                {
                    HttpClient httpClient = new HttpClient();

                    Uri uristring = new Uri(Url);
                    httpClient.Headers.Add("Content-Type", "application/json; charset=utf-8");
                    httpClient.Headers["ContentType"] = "application/json";
                    return httpClient.UploadString(Url, JsonStringParams);
                }
                catch (Exception ex)
                {
                    if (RetryCount == MaxRetryCount)
                        throw ex;
                }
            }
        }
예제 #2
0
 public void DownloadFile(String Url, String FileName)
 {
     for (int RetryCount = 1 ; true ; RetryCount++)
     {
         try
         {
             HttpClient httpClient = new HttpClient();
             httpClient.DownloadFile(Url, FileName);
             return;
         }
         catch (Exception ex)
         {
             if (RetryCount == MaxRetryCount)
                 throw ex;
         }
     }
 }
예제 #3
0
        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;
                }
            }
        }
예제 #4
0
        public HtmlDocument GetWebRequest(String Url)
        {
            for (int RetryCount = 1; true; RetryCount++)
            {
                try
                {
                    HttpClient httpClient = new HttpClient();

                    HtmlDocument document = new HtmlDocument();
                    document.OptionAutoCloseOnEnd = true;
                    document.OptionCheckSyntax = true;
                    document.OptionFixNestedTags = true;
                    //document.OptionWriteEmptyNodes = true;
                    byte[] responseBytes;
                    if (EnableCaching && !cache.IsCachedUrl(Url))
                    {
                        responseBytes = httpClient.DownloadData(Url);
                        CacheDb.SaveCache(Url, responseBytes);
                    }
                    else if (EnableCaching)
                        responseBytes = cache.GetCachedUrl(Url);
                    else
                        responseBytes = httpClient.DownloadData(Url);
                    MemoryStream mStream = new MemoryStream(responseBytes);
                    document.Load(mStream);
                    return document;
                }
                catch (Exception ex)
                {
                    if (RetryCount == MaxRetryCount)
                        throw ex;
                }
            }
        }