Advertisement
Guest User

Untitled

a guest
Jul 18th, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.82 KB | None | 0 0
  1. // Для того что бы скачать страницу какого-либо сайта
  2. string page;
  3. using (var client = new WebClient()) {
  4.     page = client.DownloadString("http://www.example.com/");
  5. }
  6.  
  7. // Файл http.cs:
  8. using System;
  9. using System.Collections.Generic;
  10. using System.Linq;
  11. using System.Text;
  12. using SeasideResearch.LibCurlNet;
  13. using System.IO;
  14.  
  15. namespace namespace
  16. {
  17.     class http
  18.     {
  19.         private static Easy easy;
  20.         private static Random rand = new Random();
  21.         private static string SockBuff;
  22.         private static string CookieFile = AppDomain.CurrentDomain.BaseDirectory + "cookie" + rand.Next(0, 9) + rand.Next(0, 9) + rand.Next(0, 9) + rand.Next(0, 9) + rand.Next(0, 9) + rand.Next(0, 9) + rand.Next(0, 9) + ".txt";
  23.         public static string UserAgent = "Mozilla/5.0 (Windows NT 6.3; Trident/7.0; rv:11.0) like Gecko";
  24.         public static string Proxy = "";
  25.         public string referer = "";
  26.  
  27.         public void Dispose()
  28.         {
  29.             ClearCookies();
  30.         }
  31.         public string getCookieFile()
  32.         {
  33.             return CookieFile;
  34.         }
  35.  
  36.         public void CurlInit()
  37.         {
  38.             Curl.GlobalInit((int)CURLinitFlag.CURL_GLOBAL_ALL);
  39.         }
  40.  
  41.         public void ClearCookies()
  42.         {
  43.             if (File.Exists(CookieFile))
  44.             {
  45.                 File.Delete(CookieFile);
  46.             }
  47.  
  48.         }
  49.  
  50.         public string HTTPGet(string URL, string Proxy)
  51.         {
  52.             easy = new Easy();
  53.             SockBuff = "";
  54.  
  55.             try
  56.             {
  57.                 Easy.WriteFunction wf = new Easy.WriteFunction(OnWriteData);
  58.  
  59.                 easy.SetOpt(CURLoption.CURLOPT_URL, URL);
  60.                 easy.SetOpt(CURLoption.CURLOPT_TIMEOUT, "60");
  61.                 easy.SetOpt(CURLoption.CURLOPT_WRITEFUNCTION, wf);
  62.                 easy.SetOpt(CURLoption.CURLOPT_USERAGENT, UserAgent);
  63.                 easy.SetOpt(CURLoption.CURLOPT_COOKIEFILE, CookieFile);
  64.                 easy.SetOpt(CURLoption.CURLOPT_COOKIEJAR, CookieFile);
  65.                 easy.SetOpt(CURLoption.CURLOPT_FOLLOWLOCATION, true);
  66.  
  67.                 if (URL.Contains("https"))
  68.                 {
  69.                     easy.SetOpt(CURLoption.CURLOPT_SSL_VERIFYHOST, 1);
  70.                     easy.SetOpt(CURLoption.CURLOPT_SSL_VERIFYPEER, 0);
  71.                 }
  72.  
  73.                 if (Proxy != "")
  74.                 {
  75.                     easy.SetOpt(CURLoption.CURLOPT_PROXY, Proxy);
  76.                     easy.SetOpt(CURLoption.CURLOPT_PROXYTYPE, CURLproxyType.CURLPROXY_HTTP);
  77.                 }
  78.  
  79.                 easy.Perform();
  80.                 easy.Cleanup();
  81.  
  82.             }
  83.             catch
  84.             {
  85.                 Console.WriteLine("Get Request Error");
  86.             }
  87.  
  88.             return SockBuff;
  89.         }
  90.  
  91.         public string HTTPPost(string URL, string http_headers, string Content)
  92.         {
  93.             easy = new Easy();
  94.             SockBuff = "";
  95.             String proxy;
  96.  
  97.             try
  98.             {
  99.                 Easy.WriteFunction wf = new Easy.WriteFunction(OnWriteData);
  100.  
  101.                 easy.SetOpt(CURLoption.CURLOPT_URL, URL);
  102.                 easy.SetOpt(CURLoption.CURLOPT_TIMEOUT, "60");
  103.                 easy.SetOpt(CURLoption.CURLOPT_WRITEFUNCTION, wf);
  104.                 easy.SetOpt(CURLoption.CURLOPT_USERAGENT, UserAgent);
  105.                 easy.SetOpt(CURLoption.CURLOPT_COOKIEFILE, CookieFile);
  106.                 easy.SetOpt(CURLoption.CURLOPT_COOKIEJAR, CookieFile);
  107.                 easy.SetOpt(CURLoption.CURLOPT_HTTPHEADER, http_headers);
  108.                 easy.SetOpt(CURLoption.CURLOPT_REFERER, referer);
  109.                 //easy.SetOpt(CURLoption.CURLOPT_POSTFIELDSIZE, Content.Length);
  110.                 easy.SetOpt(CURLoption.CURLOPT_FOLLOWLOCATION, true);
  111.                 easy.SetOpt(CURLoption.CURLOPT_STDERR, "test.txt");
  112.  
  113.                 easy.SetOpt(CURLoption.CURLOPT_HTTPHEADER, 1);
  114.  
  115.                 easy.SetOpt(CURLoption.CURLOPT_POST, true);
  116.                 easy.SetOpt(CURLoption.CURLOPT_POSTFIELDS, Content);
  117.  
  118.  
  119.                 if (URL.Contains("https"))
  120.                 {
  121.                     easy.SetOpt(CURLoption.CURLOPT_SSL_VERIFYHOST, 1);
  122.                     easy.SetOpt(CURLoption.CURLOPT_SSL_VERIFYPEER, 0);
  123.                 }
  124.  
  125.                 if (Proxy != "")
  126.                 {
  127.                     easy.SetOpt(CURLoption.CURLOPT_PROXY, Proxy);
  128.                     easy.SetOpt(CURLoption.CURLOPT_PROXYTYPE, CURLproxyType.CURLPROXY_HTTP);
  129.                 }
  130.  
  131.                 easy.Perform();
  132.                 easy.Cleanup();
  133.  
  134.             }
  135.             catch
  136.             {
  137.  
  138.             }
  139.             return SockBuff;
  140.  
  141.         }
  142.  
  143.         public string SafeString(string data)
  144.         {
  145.             return Curl.Escape(data, data.Length);
  146.         }
  147.  
  148.         public string UnSafeString(string data)
  149.         {
  150.             return Curl.Unescape(data, data.Length);
  151.         }
  152.  
  153.         public static Int32 OnWriteData(Byte[] buf, Int32 size, Int32 nmemb, Object extraData)
  154.         {
  155.             // Console.Write(System.Text.Encoding.UTF8.GetString(  buf));
  156.             SockBuff = SockBuff + System.Text.Encoding.UTF8.GetString(buf);
  157.  
  158.             return size * nmemb;
  159.         }
  160.     }
  161. }
  162.  
  163. // Файл Form1.cs:
  164. http httpVar = new http();
  165. httpVar.CurlInit(); // cURL init
  166. httpVar.ClearCookies();
  167.  
  168. String
  169. http_headers = "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8";
  170. http_headers = http_headers + "User-Agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36";
  171. http_headers = http_headers + "Accept-Encoding: gzip,deflate,sdch";
  172. http_headers = http_headers + "Accept-Language: ru-RU,ru;q=0.8,en-US;q=0.6,en;q=0.4";
  173.  
  174. string htmlCode = httpVar.HTTPGet(tbAdress.Text, "");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement