C# WebBrowser entering data to get the TIN of a physical person.persons on the tax service website

I'm trying to write a program that would go to the site https://service.nalog.ru/inn.do, filled out the form, and received the TIN, if everything was filled out correctly. The problem is that when you first log in to the site, you are asked if I agree with the processing of personal data ( there is a check box and a button), after clicking "Agree", the site redirects to the main page of the service. After clicking on the "Agree" button, it displays an error that the last name field was not found, but it is clear that the redirect didn't happen apparently. What am I doing wrong?

private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
    {

    }
    private void button1_Click(object sender, EventArgs e)
    {
        WebBrowser br = new WebBrowser();
        br.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webBrowser1_DocumentCompleted);
        br.Navigate("https://service.nalog.ru/inn.do");
        while (br.ReadyState != WebBrowserReadyState.Complete)
        {
            Application.DoEvents();
        }
        br.Document.GetElementById("personalData").SetAttribute("checked", "checked");
        HtmlElementCollection elc = br.Document.GetElementsByTagName("button");
        foreach (HtmlElement el in elc)
        {
            if (el.GetAttribute("type").Equals("button"))
            {
                el.InvokeMember("Click");
            }
        }
        while (br.ReadyState != WebBrowserReadyState.Complete)
        {
            Application.DoEvents();
        }
        Console.WriteLine("Natigated to {1}");
        Console.WriteLine(br.Document.Body.InnerHtml);
        br.Document.GetElementById("fam").InnerText = "Иванов";
        br.Document.GetElementById("nam").InnerText = "Иван";
        br.Document.GetElementById("otch").InnerText = "Иванович";
        br.Document.GetElementById("bdate").InnerText = "11.11.1111";
        br.Document.GetElementById("doctype").SetAttribute("value", "21");
        br.Document.GetElementById("docno").InnerText = "00 00 000000";
        br.Document.GetElementById("btn_Send").InvokeMember("click");
        Console.WriteLine(br.Document.GetElementById("resultInn").InnerText);

        Console.WriteLine(br.Document.Body.InnerHtml);

    }
Author: Andrey Sherman, 2019-09-16

1 answers

In general, thanks to the help of @EvgeniyZ, I refused to use WebBrowser, and wrote maybe bad code, but still working. So if someone needs to get data from service.nalog.ru/inn.do then here:

[DataContract]
public class fns
{   
    [DataMember]
    public string inn { get; set; }
    [DataMember]
    public bool captchaRequired { get; set; }
    [DataMember]
    public int code { get; set; }
}

class Program
{
    static void Main(string[] args)
    {


        WebRequest request = WebRequest.Create("https://service.nalog.ru/inn-proc.do");
        request.Method = "POST"; // для отправки используется метод Post
                                 // данные для отправки
        string data = "c=innMy&captcha=&captchaToken=&fam=Иванов&nam=Иван&otch=Иванович&bdate=29.08.1988&bplace=&doctype=21&docno=01+01+2000&docdt";
        // преобразуем данные в массив байтов
        byte[] byteArray = System.Text.Encoding.UTF8.GetBytes(data);
        // устанавливаем тип содержимого - параметр ContentType
        request.ContentType = "application/x-www-form-urlencoded";
        // Устанавливаем заголовок Content-Length запроса - свойство ContentLength
        request.ContentLength = byteArray.Length;

        //записываем данные в поток запроса
        using (Stream dataStream = request.GetRequestStream())
        {
            dataStream.Write(byteArray, 0, byteArray.Length);
        }

        WebResponse response = request.GetResponse();
        using (Stream stream = response.GetResponseStream())
        {
            using (StreamReader reader = new StreamReader(stream))
            {
                DataContractJsonSerializer deserializer = new DataContractJsonSerializer(typeof(fns));

                fns res = (fns)deserializer.ReadObject(stream);
                Console.WriteLine(res.inn);
            }
        }



        response.Close();

        Console.ReadKey();
    }

}

 1
Author: Andrey Sherman, 2019-09-16 19:02:53