K
K
kos_dev2017-07-05 06:50:52
C++ / C#
kos_dev, 2017-07-05 06:50:52

How to bind cookies to HttpWebRequest [C#]?

Hello, tell me how to enter the site already logged in, that is, with cookies.
I tried to log into the account in the browser, look at all the cookies, rewrite them in software and download the page for further parsing, but the cookies do not respond.
My task is to download a page that is displayed to authorized users.
Thank you all in advance for your help.
THE CODE:

CookieContainer cookieContainer = new CookieContainer();

cookieContainer.Add( new Cookie("_ym_uid", "149922490167632560", "/", ".domkadrov.ru") );
cookieContainer.Add(new Cookie("_ym_isad", "2", "/", ".domkadrov.ru"));
cookieContainer.Add(new Cookie("_gat", "1", "/", ".domkadrov.ru"));
cookieContainer.Add(new Cookie("forads", "%CF%F0%EE%E8%E7%E2%EE%E4%F1%F2%E2%EE+%E1%F3%EC%E0%E3%E8%2C+%EA%E0%F0%F2%EE%ED%E0", "/", "www.domkadrov.ru"));
cookieContainer.Add(new Cookie("passwd[423989]", "ghjcnj", "/", "www.domkadrov.ru"));
cookieContainer.Add(new Cookie("employer", "423989", "/", "www.domkadrov.ru"));
cookieContainer.Add(new Cookie("test", "%F0%F3%F1%F1%EA%E8%E9", "/", "www.domkadrov.ru"));
cookieContainer.Add(new Cookie("device", "desktop", "/", "www.domkadrov.ru"));
cookieContainer.Add(new Cookie("views", "11", "/", "www.domkadrov.ru"));
cookieContainer.Add(new Cookie("cityforpublish", "378", "/", "www.domkadrov.ru"));
cookieContainer.Add(new Cookie("compfunction", "139b", "/", "www.domkadrov.ru"));
cookieContainer.Add(new Cookie("industry", "94", "/", "www.domkadrov.ru"));
cookieContainer.Add(new Cookie("menforads", "4", "/", "www.domkadrov.ru"));
cookieContainer.Add(new Cookie("_ga", "GA1.2.1762919872.1499224902", "/", ".domkadrov.ru"));
cookieContainer.Add(new Cookie("_gid", "GA1.2.562227849.1499224902", "/", ".domkadrov.ru"));


HttpWebRequest proxy_request = (HttpWebRequest)WebRequest.Create("http://www.jobinmoscow.ru/");
proxy_request.CookieContainer = cookieContainer;
proxy_request.Method = "GET";
proxy_request.ContentType = "application/x-www-form-urlencoded";
proxy_request.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/532.5 (KHTML, like Gecko) Chrome/4.0.249.89 Safari/532.5";
proxy_request.KeepAlive = true;
HttpWebResponse resp = proxy_request.GetResponse() as HttpWebResponse;
string html = "";
using (StreamReader sr = new StreamReader(resp.GetResponseStream(), Encoding.UTF8))
    html = sr.ReadToEnd();

foreach (Cookie cook in resp.Cookies)
{
    textBox1.Text += "Name: " + cook.Name + " Val: " + cook.Value + Environment.NewLine;
}

html = html.Trim();

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Z
Zakharov Alexander, 2017-07-05
@AlexZaharow

Most likely you have not entered the necessary cookies (usually the required number of cookies is calculated in units). I advise you to run the pages through fiddler and look at all the cookies there, not on the page.
Since you still have a problem with a higher-order task - parsing a logged in user, I advise you to use not C # in its pure form, but a browser component. For simple cases, use c# WebBrowser, log in as a user, and then parse everything that is loaded (in the same place, the initial scripts are loaded and change the DOM). For complex cases, CefSharp - Chrome for C# is great. This beast very obediently does everything that you order him. I periodically upload documentation for my company on it from the site of the guests, for example.
Here, I found an interesting example: zennolab.com/discussion/threads/c-sharp-vs-webbrow...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question