Авторизация на сайте - C# (195575)
Формулировка задачи:
В devel studio использовал такой код для авторизации и отправки вопроса на ask.fm
Пришлось уйти из devel studio, там не получилось сделать выполнение в "реальном времени" в C# есть команда хорошая
альтернативы на devel studio не нашел(
что может перевести код из php в c# буду оч благодарен
Листинг программы
- define('SETUSERAGENT', 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1)');
- function getToken() {
- $a = curl_init("http://ask.fm/vitafon007");
- curl_setopt($a, CURLOPT_USERAGENT, SETUSERAGENT);
- curl_setopt($a, CURLOPT_COOKIEJAR, 'cookie.txt');
- curl_setopt($a, CURLOPT_COOKIEFILE, 'cookie.txt');
- curl_setopt($a, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt($a, CURLOPT_CONNECTTIMEOUT, 10);
- curl_setopt($a, CURLOPT_MAXREDIRS, 10);
- curl_setopt($a, CURLOPT_REFERER, 'http://ask.fm/');
- curl_setopt($a, CURLOPT_FOLLOWLOCATION, 0);
- curl_setopt($a, CURLOPT_HEADER, FALSE);
- curl_setopt($a, CURLOPT_SSL_VERIFYPEER, 0);
- curl_setopt($a, CURLOPT_SSL_VERIFYHOST, 0);
- $wynik = curl_exec($a);
- curl_close($a);
- preg_match_all('#<input name="authenticity_token" type="hidden" value="(.*)" />#', $wynik, $token);
- return $token[1][0];
- }
- function login($login, $password) {
- $token = getToken();
- $curlchanel = curl_init("http://ask.fm/session");
- curl_setopt($curlchanel, CURLOPT_USERAGENT, SETUSERAGENT);
- curl_setopt($curlchanel, CURLOPT_COOKIEJAR, 'cookie.txt');
- curl_setopt($curlchanel, CURLOPT_COOKIEFILE, 'cookie.txt');
- curl_setopt($curlchanel, CURLOPT_HEADER, 0);
- curl_setopt($curlchanel, CURLOPT_TIMEOUT, 6);
- curl_setopt($curlchanel, CURLOPT_SSL_VERIFYPEER, 0);
- curl_setopt($curlchanel, CURLOPT_SSL_VERIFYHOST, 0);
- curl_setopt($curlchanel, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt($curlchanel, CURLOPT_POSTFIELDS, trim("authenticity_token=$token&login=$login&password=$password&follow=&like=&back=&authenticity_token=$token"));
- $wynik = curl_exec($curlchanel);
- curl_close($curlchanel);
- }
- function ask($question, $user) {
- $question = urlencode(iconv ("CP1251","UTF-8",$question))
- $token = getToken();
- $curlchanel = curl_init("http://ask.fm/$user/questions/create");
- curl_setopt($curlchanel, CURLOPT_USERAGENT, SETUSERAGENT);
- curl_setopt($curlchanel, CURLOPT_COOKIEJAR, 'cookie.txt');
- curl_setopt($curlchanel, CURLOPT_COOKIEFILE, 'cookie.txt');
- curl_setopt($curlchanel, CURLOPT_HEADER, 0);
- curl_setopt($curlchanel, CURLOPT_TIMEOUT, 6);
- curl_setopt($curlchanel, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt($curlchanel, CURLOPT_SSL_VERIFYPEER, 0);
- curl_setopt($curlchanel, CURLOPT_SSL_VERIFYHOST, 0);
- curl_setopt($curlchanel, CURLOPT_POSTFIELDS, trim("authenticity_token=$token&question%5Bquestion_text%5D=$question%3F&question%5Bforce_anonymous%5D=&question%5Bforce_anonymous%5D=force_anonymous&authenticity_token=$token"));
- $wynik = curl_exec($curlchanel);
- curl_close($curlchanel);
- }
Листинг программы
- Application.DoEvents();
Решение задачи: «Авторизация на сайте»
textual
Листинг программы
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Net;
- using System.IO;
- namespace parser
- {
- class Program
- {
- static string getResponse(string uri)
- {
- StringBuilder sb = new StringBuilder();
- byte[] buf = new byte[8192];
- HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
- HttpWebResponse response = (HttpWebResponse)request.GetResponse();
- Stream resStream = response.GetResponseStream();
- int count = 0;
- do
- {
- count = resStream.Read(buf, 0, buf.Length);
- if (count != 0)
- {
- sb.Append(Encoding.Default.GetString(buf, 0, count));
- }
- }
- while (count > 0);
- return sb.ToString();
- }
- static private void addd(string line)
- {
- System.IO.StreamWriter writer = new System.IO.StreamWriter("pars.txt", true);
- writer.WriteLine(line);
- writer.Close();
- }
- static void Main(string[] args)
- {
- string porovnat = " ";
- int shet=0;
- string zapis;
- int index=1;
- try
- {
- for (int i2 = 1; i2 < 100000; i2++)
- {
- string t = getResponse("http://market.yandex.ua/guru.xml?hid=91491&CMD=-RR=9,0,0,0-VIS=270-CAT_ID=160043-BPOS=" + i2 + "0-EXC=1-PG=10&greed_mode=false");
- string tm = t;
- if (t == porovnat || index <= 0)
- break;
- for (int i = 0; i < 10; i++)
- {
- index = t.IndexOf("<a id=\"item-href");
- if (index <= 0)
- {
- shet++;
- Console.WriteLine("error index" + shet.ToString());
- break;
- }
- t = t.Substring(index, t.Length - index);
- index = t.IndexOf("</a>");
- zapis = t.Substring(0, index + 4);
- addd(zapis);
- t = t.Substring(index, t.Length - index);
- porovnat = tm;
- }
- }
- }
- catch(Exception e)
- {
- Console.WriteLine(e);
- Console.ReadLine();
- }
- System.Threading.Thread.Sleep(100);
- }
- }
- }
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д