How do you access a web page and get its content in C #?

How do you enter a web page and upload its content to C #?

+3
source share
7 answers
string postData = "userid=ducon";
            postData += "&username=camarche" ;
            byte[] data = Encoding.ASCII.GetBytes(postData);
            WebRequest req = WebRequest.Create(
                URL);
            req.Method = "POST";
            req.ContentType = "application/x-www-form-urlencoded";
            req.ContentLength = data.Length;
            Stream newStream = req.GetRequestStream();
            newStream.Write(data, 0, data.Length);
            newStream.Close();
            StreamReader reader = new StreamReader(req.GetResponse().GetResponseStream(), System.Text.Encoding.GetEncoding("iso-8859-1"));
            string coco = reader.ReadToEnd();
+2
source

, . - ( , GET POST), cookie. , - cookie, POST , -, , , .

+5

System.Net.WebClient System.Net.HttpWebRequest/System.Net.HttpWebResponse.

: html , , , Http-.

+3

"login"?

, , Credentials HttpWebRequest.

- / cookie, HttpWebRequest .

+2

WebClient.

Dim Html As String

Using Client As New System.Net.WebClient()
    Html = Client.DownloadString("http://www.google.com")
End Using
+1

WebClient , .

WebClient wc = new WebClient();
wc.Credentials = new NetworkCredential("username", "password");
string url = "http://foo.com";          
try
{
    using (Stream stream = wc.OpenRead(new Uri(url)))
    {
        using (StreamReader reader = new StreamReader(stream))
        {
            return reader.ReadToEnd();
             }
    }
}
catch (WebException e)
{
    //Error handeling
}
+1

:

public string GetContent(string url)  
{ 
  using (System.Net.WebClient client =new System.Net.WebClient()) 
  { 
  return client.DownloadString(url); 
  } 
} 
-2

Source: https://habr.com/ru/post/1704207/


All Articles