Здравствуйте, господа! Я довольно недавно пишу на C# .NET, и вот на днях решил попробовать сделать несложного робота для разбора информации на некоем сайте, использующем авторизацию на основе cookies. Пытаюсь использовать для этого класс HttpWebRequest. Однако при этом столкнулся со следующей проблемой. Поясню на примере фрагмента кода:
Код: String postData = String.Format("a=logon&back=&login={0}&password={1}&submit=войти",
HttpUtility.UrlEncode(txtLogin.Text), HttpUtility.UrlEncode(txtPassword.Text));
CookieContainer cookies = new CookieContainer();
HttpWebRequest request = WebRequest.Create(txtURL.Text) as HttpWebRequest;
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.CookieContainer = cookies;
using (StreamWriter requestStreamWriter = new StreamWriter(request.GetRequestStream()))
{
requestStreamWriter.Write(postData);
}
using (StreamReader readStream = new StreamReader(request.GetResponse().GetResponseStream(), Encoding.UTF8))
{
using (StreamWriter sw = new StreamWriter("page.html"))
{
sw.Write(readStream.ReadToEnd());
}
}
IFormatter formatter = new BinaryFormatter();
Stream stream = new FileStream("cookies.tmp", FileMode.Create, FileAccess.Write, FileShare.None);
formatter.Serialize(stream, cookies);
stream.Close();
Код: String postData = String.Format("a=logon&back=&login={0}&password={1}&submit=войти",
HttpUtility.UrlEncode(txtLogin.Text), HttpUtility.UrlEncode(txtPassword.Text));
CookieContainer cookies = new CookieContainer();
HttpWebRequest request = WebRequest.Create(txtURL.Text) as HttpWebRequest;
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.CookieContainer = cookies;
using (StreamWriter requestStreamWriter = new StreamWriter(request.GetRequestStream()))
{
requestStreamWriter.Write(postData);
}
using (StreamReader readStream = new StreamReader(request.GetResponse().GetResponseStream(), Encoding.UTF8))
{
using (StreamWriter sw = new StreamWriter("page.html"))
{
sw.Write(readStream.ReadToEnd());
}
}
IFormatter formatter = new BinaryFormatter();
Stream stream = new FileStream("cookies.tmp", FileMode.Create, FileAccess.Write, FileShare.None);
formatter.Serialize(stream, cookies);
stream.Close();