and while I found a lot of articles and other information on how to use GET and POST with HttpWebRequest and Response, it’s hard for me to work on so that I can expect them to work.
I play with a few ideas that I found, but still nothing works ... I will post my code:
private void start_post()
{
string username = txtUser.Text;
string password = txtPassword.Text;
string strResponce;
byte[] buffer = Encoding.ASCII.GetBytes("username="+username+"&password="+password);
HttpWebRequest WebReq = (HttpWebRequest)WebRequest.Create(txtLink.Text);
WebReq.Method = "POST";
WebReq.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)";
WebReq.Headers.Add("Translate", "F");
WebReq.AllowAutoRedirect = true;
WebReq.CookieContainer = cookieJar;
WebReq.KeepAlive = true;
WebReq.ContentType = "application/x-www-form-urlencoded";
WebReq.ContentLength = buffer.Length;
Stream PostData = WebReq.GetRequestStream();
PostData.Write(buffer, 0, buffer.Length);
PostData.Close();
HttpWebResponse WebResp = (HttpWebResponse)WebReq.GetResponse();
Stream answer = WebResp.GetResponseStream();
StreamReader _answer = new StreamReader(answer);
strResponce = _answer.ReadToEnd();
answer.Close();
_answer.Close();
foreach (Cookie cookie in WebResp.Cookies)
{
cookieJar.Add(new Cookie(cookie.Name.Trim(), cookie.Value.Trim(), cookie.Path, cookie.Domain));
txtResult.Text += cookie.Name.ToString() + Environment.NewLine + cookie.Value.ToString() + Environment.NewLine + cookie.Path.ToString() + Environment.NewLine + cookie.Domain.ToString();
}
if (strResponce.Contains("Log On Successful") || strResponce.Contains("already has a webseal session"))
{
MessageBox.Show("Login success");
foreach (Control cont in this.Controls)
{
cont.Visible = true;
}
}
else
{
MessageBox.Show("Login Failed.");
}
}
Here, in the code, I can completely get to the end and still could not get the login when I go to http://www.comicearth.com (my own site, php and apache) I created a form and from this form I enter password and username. When he does this, he says that failed, which is good. I also use Fidder to see that everything is happening.
, , - .
, -, :
HttpWebResponse WebResp = (HttpWebResponse)WebReq.GetResponse();
"Content-Length Chunked Encoding , ".
, , , , - 302...
, Fiddler, , -. , , , .
- , -, , , , , . , , cookie .. , - , , , , , .
, , , httpwebrequest httpwebresponse, -.
htmlagilitypack, , 100% , .
- , , -, , .
.
. : - , , - : "Content-Length Chunked Encoding .....", allowAutoRedirect = false, "location" .., , , , , .: S
private void start_post2()
{
string username = txtUser.Text;
string password = txtPassword.Text;
Uri link = new Uri(txtLink.Text);
string postArgs = string.Format(@"userId={0}&password={1}", username, password);
byte[] buffer = Encoding.ASCII.GetBytes(postArgs);
HttpWebRequest WebReq = (HttpWebRequest)WebRequest.Create(txtLink.Text);
WebReq.Method = "POST";
WebReq.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)";
WebReq.AllowAutoRedirect = false;
WebReq.Accept = "application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5";
WebReq.Accept = "*/*";
WebReq.CookieContainer = cookieJar;
WebReq.KeepAlive = true;
WebReq.ContentType = "application/x-www-form-urlencoded";
WebReq.ContentLength = buffer.Length;
Stream PostData = WebReq.GetRequestStream();
PostData.Write(buffer, 0, buffer.Length);
PostData.Close();
HttpWebResponse WebResp = (HttpWebResponse)WebReq.GetResponse();
if (WebResp == null) throw new Exception("Response is null");
foreach (Cookie cookie in WebResp.Cookies)
{
cookieJar.Add(new Cookie(cookie.Name.Trim(), cookie.Value.Trim(), cookie.Path, cookie.Domain));
}
if (!string.IsNullOrEmpty(WebResp.Headers["Location"]))
{
string newLocation = WebResp.Headers["Location"];
WebReq = (HttpWebRequest)WebRequest.Create(newLocation);
WebReq.Method = "GET";
WebReq.ContentType = "application/x-www-form-unlencoded";
WebReq.AllowAutoRedirect = false;
WebReq.CookieContainer = cookieJar;
WebReq.CookieContainer.Add(WebResp.Cookies);
buffer = Encoding.ASCII.GetBytes("userId=" + username + "&password=" + password);
WebReq.ContentLength = buffer.Length;
PostData = WebReq.GetRequestStream();
PostData.Write(buffer, 0, buffer.Length);
PostData.Close();
WebResp = (HttpWebResponse)WebReq.GetResponse();
foreach (Cookie cookie in WebResp.Cookies)
{
cookieJar.Add(new Cookie(cookie.Name.Trim(), cookie.Value.Trim(), cookie.Path, cookie.Domain));
}
}
else if (!string.IsNullOrEmpty(WebResp.Headers["Set-Cookie"]))
{
}
foreach (Cookie cookie in cookieJar.GetCookies(link))
{
MessageBox.Show(cookie.Name.ToString() + Environment.NewLine + cookie.Value.ToString() + Environment.NewLine + cookie.Path.ToString() + Environment.NewLine + cookie.Domain.ToString());
}
StreamReader sr = new StreamReader(WebResp.GetResponseStream());
string responseHtml = sr.ReadToEnd().Trim();
SearchPatient(WebReq, username, password);
}