Головна‎ > ‎C#‎ > ‎Додатково‎ > ‎

Закачати сайт

Найпростіший

using System.Net;

using (WebClient wc = new WebClient()){
// wc.Encoding = Encoding.UTF8;  
// Типове ANSI - подвоює скісні риски в адресах
string a = wc.DownloadString("http://...");
}



Юнікод

WebRequest request = WebRequest.Create( "http://..."); 
request.Credentials = CredentialCache.DefaultCredentials; 
WebResponse response = request.GetResponse(); 
Console.WriteLine(((HttpWebResponse)response).StatusDescription); // статус
Stream dataStream = response.GetResponseStream(); 
StreamReader reader = new StreamReader(dataStream); // відкриваємо потік
string responseFromServer = reader.ReadToEnd(); 
Console.WriteLine(responseFromServer); 
reader.Close(); 
response.Close();



Або так

WebRequest request = WebRequest.Create("http://...");
WebResponse response = request.GetResponse();
using (Stream stream = response.GetResponseStream())
{
   using (StreamReader reader = new StreamReader(stream))
   {
      string line = "";
      while ((line = reader.ReadLine()) != null)
      {
         Console.WriteLine(line);
      }
   }
}
response.Close();




Куки

using xNet;

Get(){
     HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
     req.Method = "GET";
     req.CookieContainer = cookies;
     req.UserAgent = Http.ChromeUserAgent();
     req.Referer = referer;
     req.KeepAlive = false;
     ServicePointManager.Expect100Continue = false;
     HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
     cookies.Add(resp.Cookies);

     using (StreamReader sr = new StreamReader(resp.GetResponseStream()))
     {
          src = sr.ReadToEnd();
     }
}

CookieContainer mycookie = new CookieContainer();
string src = Get("https://test.org/", "https://test.org/login/", mycookie);