Авторизация на сайте Cookie - C#
Формулировка задачи:
Здравствуйте, мне нужно авторизоваться на сайте и далее посылать пост запросы с авторизованного аккаунта
Вот что получаю от google chrome при авторизации
Так авторизуюсь
Но почему то авторизация не проходит
Так передаю пост-запрос
Почему не удается авторизоваться? И как получить и подключить куки к пост-запросу?
Листинг программы
- Request URL:http://xxxx.biz/login/login
- Request Method:POST
- Status Code:303 See Other
- Remote Address:176.9.136.59:80
- Response Headers
- HTTP/1.1 303 See Other
- Server: nginx
- Date: Sun, 08 May 2016 04:24:59 GMT
- Content-Type: text/html;charset=UTF-8
- Content-Length: 0
- Connection: keep-alive
- Cache-control: private, max-age=0
- Expires: Thu, 19 Nov 1981 08:52:00 GMT
- Last-Modified: Sun, 08 May 2016 04:24:59 GMT
- Location: http://xxxx.biz/
- Set-Cookie: xf_user=1%2C594a06e721ac2dc1dcc7714cfbeddd89f064d025; expires=Tue, 07-Jun-2016 04:24:58 GMT; path=/; httponly
- Set-Cookie: xf_session=6c13ff2937edee51043df5eb61594a67; path=/; httponly
- X-Frame-Options: SAMEORIGIN
- X-Powered-By: PHP/5.3.3
- X-Robots-Tag: none
- Request Headers
- POST /login/login HTTP/1.1
- Host: ojrs243un5zgkltcnf5a.cmle.ru
- Connection: keep-alive
- Content-Length: 114
- Cache-Control: max-age=0
- Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
- Origin: http://ojrs243un5zgkltcnf5a.cmle.ru
- Upgrade-Insecure-Requests: 1
- User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.94 Safari/537.36
- Content-Type: application/x-www-form-urlencoded
- Referer: http://xxxx.biz/
- Accept-Encoding: gzip, deflate
- Accept-Language: ru-RU,ru;q=0.8,en-US;q=0.6,en;q=0.4
- Cookie: crtg_rta=; _gat=1; xf_session=d8719b6c9692ab795b6ae3c45dfd24d8; _ga=GA1.2.783234481.1460544443
- Form Data
- login=admin®ister=0&password=XXXXXX&remember=1&cookie_check=1&_xfToken=&redirect=http%3A%2F%2Fxxxx.biz%2F
Листинг программы
- WebRequest request = WebRequest.Create("http://xxx.biz/login/login");
- request.Method = "POST";
- string postData = "login=admin®ister=0&password=xxxxxxxxx&remember=1&cookie_check=1&_xfToken=&redirect=http%3A%2F%2Fxxxx.biz%2F";
- byte[] byteArray = Encoding.GetEncoding("windows-1251").GetBytes(postData);
- request.ContentType = "application/x-www-form-urlencoded";
- request.ContentLength = byteArray.Length;
- Stream dataStream = request.GetRequestStream();
- dataStream.Write(byteArray, 0, byteArray.Length);
- dataStream.Close();
- WebResponse response = request.GetResponse();
- dataStream = response.GetResponseStream();
- StreamReader reader = new StreamReader(dataStream);
- string responseFromServer = reader.ReadToEnd();
- //MessageBox.Show(responseFromServer);
- textBox1.Text = responseFromServer;
- reader.Close();
- dataStream.Close();
- response.Close();
Листинг программы
- WebRequest request = WebRequest.Create(textBox6.Text);
- request.Method = "POST";
- string nam = WebUtility.UrlEncode(name[i]);
- string mes = WebUtility.UrlEncode(message[i]);
- string postData = "_guestUsername=" + nam + "&message_html=" + mes + "&_xfRelativeResolver=" + textBox6.Text + "%2Freply&_xfToken=&_xfRequestUri=%2Fthreads%2Fxxxx.1470%2Freply&_xfNoRedirect=1&_xfResponseType=json";
- //_guestUsername=nam&message_html=" + mes + "&_xfRelativeResolver=" + textBox6.Text + "%2Freply&_xfToken=&_xfRequestUri=%2Fthreads%2Fxxxx.1470%2Freply&_xfNoRedirect=1&_xfResponseType=json
- byte[] byteArray = Encoding.GetEncoding("windows-1251").GetBytes(postData);
- request.ContentType = "application/x-www-form-urlencoded";
- request.ContentLength = byteArray.Length;
- Stream dataStream = request.GetRequestStream();
- dataStream.Write(byteArray, 0, byteArray.Length);
- dataStream.Close();
- WebResponse response = request.GetResponse();
- dataStream = response.GetResponseStream();
- StreamReader reader = new StreamReader(dataStream);
- string responseFromServer = reader.ReadToEnd();
- reader.Close();
- dataStream.Close();
- response.Close();
При авторизации в ответе нашел такую вот строчку "Чтобы войти на сайт, в Вашем браузере должны быть включены cookie. Вы не сможете войти, пока они выключены."
Решение задачи: «Авторизация на сайте Cookie»
textual
Листинг программы
- request.CookieContainer = cookies;
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д