.NET 4.x Post запрос к сайту - C#
Формулировка задачи:
Здравствуйте, пробую авторизоваться на сайте adm.trudvsem.ru, прописываю параметры, но мне возвращается страница авторизации
Скриншот возврата
Скриншот анализа страницы бразером
Листинг программы
- private void button1_Click(object sender, EventArgs e)
- {
- string gotov;
- System.Net.WebRequest req = System.Net.WebRequest.Create("https://adm.trudvsem.ru/login?0-1.IFormSubmitListener-signInForm");
- req.Method = "POST";
- req.Timeout = 100000;
- req.ContentType = "application/x-www-form-urlencoded";
- byte[] sentData = Encoding.GetEncoding(1251).GetBytes("username=tokru&password=dd3n");
- req.ContentLength = sentData.Length;
- System.IO.Stream sendStream = req.GetRequestStream();
- sendStream.Write(sentData, 0, sentData.Length);
- sendStream.Close();
- System.Net.WebResponse res = req.GetResponse();
- System.IO.Stream ReceiveStream = res.GetResponseStream();
- System.IO.StreamReader sr = new System.IO.StreamReader(ReceiveStream, Encoding.UTF8);
- //Кодировка указывается в зависимости от кодировки ответа сервера
- Char[] read = new Char[256];
- int count = sr.Read(read, 0, 256);
- string Out = String.Empty;
- while (count > 0)
- {
- String str = new String(read, 0, count);
- Out += str;
- count = sr.Read(read, 0, 256);
- }
- gotov = Out;
- }
- }
- }
Решение задачи: «.NET 4.x Post запрос к сайту»
textual
Листинг программы
- System.Net.HttpWebRequest req = (System.Net.HttpWebRequest)System.Net.WebRequest.Create("https://adm.trudvsem.ru/login?0-1.IFormSubmitListener-signInForm");
- req.Method = "POST";
- req.Timeout = 100000;
- req.ContentType = "application/x-www-form-urlencoded";
- req.UserAgent = ...;
- byte[] sentData = Encoding.GetEncoding(1251).GetBytes("username=tokru&password=dd3n");
- req.ContentLength = sentData.Length;
- using (System.IO.Stream sendStream = req.GetRequestStream())
- {
- sendStream.Write(sentData, 0, sentData.Length);
- }
- string gotov;
- using (System.Net.WebResponse res = req.GetResponse())
- using (System.IO.Stream ReceiveStream = res.GetResponseStream())
- using (System.IO.StreamReader sr = new System.IO.StreamReader(ReceiveStream, Encoding.UTF8))
- {
- gotov = sr.ReadToEnd();
- }
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д