一切工作正常,但我們點擊submitData()
按鈕,我會告訴我密碼,而不是myhomepage頁,代碼工作正常,但我仍然無法登錄到IRCTC網站
所以第一個我瀏覽它的登錄頁面將設置cookie,然後被存儲在cookie的集裝箱代碼cookie是
CookieContainer cookieJar = new CookieContainer();
public Form1()
{
String captcha;
InitializeComponent();
}
private void BUTTON_LOGIN_Click(object sender, EventArgs e)
{
//Get captcha
var request = (HttpWebRequest)HttpWebRequest.Create("https://www.irctc.co.in/eticketing/loginHome.jsf");
request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8";
request.KeepAlive = true;
request.CookieContainer = cookieJar;
request.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36";
var response = request.GetResponse();
using (var stream = response.GetResponseStream())
{
}
// need array, different storage for every cookies
//
foreach (Cookie c in cookieJar.GetCookies(response.ResponseUri))
{
request.CookieContainer.Add(c);
MessageBox.Show("Cookie['" + c.Name + "']: " + c.Value);
}
}
然後我使用的cookie
private void pictureBox1_Click(object sender, EventArgs e) // This Event Refresh thr captcha
{
//Get captch
var request =(HttpWebRequest)HttpWebRequest.Create("https://www.irctc.co.in/eticketing/captchaImage");
request.Accept = "image/webp,image/*,*/*;q=0.8";
WebHeaderCollection myWebHeaderCollection = request.Headers;
myWebHeaderCollection.Add("Accept-Language", "en-US;q=0.8");
myWebHeaderCollection.Add("Upgrade-Insecure-Requests", "1");
request.KeepAlive = true;
Uri target = new Uri("https://www.irctc.co.in");
cookieJar.Add(new Cookie("language", "en_IN") { Domain = target.Host });
request.CookieContainer = cookieJar;
request.Referer = "https://www.irctc.co.in/eticketing/loginHome.jsf";
request.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36";
var response = request.GetResponse();
using (var stream = response.GetResponseStream())
{
pictureBox1.Image = Bitmap.FromStream(stream);
}
// need array, different storage for every cookies
//
foreach (Cookie c in cookieJar.GetCookies(response.ResponseUri))
{
request.CookieContainer.Add(c);
MessageBox.Show("Cookie['" + c.Name + "']: " + c.Value);
}
}
private void button1_Click(object sender, EventArgs e)
{
submitData();
}
private void displayHtml(String html)
{
webBrowser1.Navigate("about:blank");
while (webBrowser1.Document == null || webBrowser1.Document.Body == null)
Application.DoEvents();
webBrowser1.Document.OpenNew(true).Write(html);
}
用於請求驗證碼
最後我填寫captcha到文本框後提交數據,這裏是錯誤這是重新導向我的登錄頁面,所以什麼是錯的。
private void submitData()
{
try
{
String user = "myusername";
String pass = "mypassword";
String submit = "Submit";
String captcha=richTextBox1.Text.ToString() ;
ASCIIEncoding encoding = new ASCIIEncoding();
// MessageBox.Show(captcha);
string postData = "j_username=" + user + "&j_password=" + pass + "&j_captcha=" + captcha + "&submit=" + submit + "&tneg=" ;
byte[] data = Encoding.UTF8.GetBytes(postData);
Uri target = new Uri("https://www.irctc.co.in");
cookieJar.Add(new Cookie("language", "en_IN"){ Domain = target.Host });
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("https://www.irctc.co.in/eticketing/home");
request.CookieContainer = cookieJar;
WebHeaderCollection myWebHeaderCollection = request.Headers;
myWebHeaderCollection.Add("Accept-Language", "en-US;q=0.8");
myWebHeaderCollection.Add("Upgrade-Insecure-Requests", "1");
request.Method = "POST";
request.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36";
request.ContentType = "application/x-www-form-urlencode";
request.Referer = "https://www.irctc.co.in/eticketing/loginHome.jsf";
request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8";
request.ServicePoint.Expect100Continue = false;
// var bytes = Encoding.UTF8.GetBytes("action=login&from=http%3A%2F%2Fwww.dofus.com%2Ffr&login=user123&password=password1232F&remember=1");
// request.ContentLength = bytes.Length;
request.ContentLength = data.Length;
request.KeepAlive = true;
// request.AutomaticDecompression = DecompressionMethods.GZip;
// request.AutomaticDecompression = DecompressionMethods.Deflate;
// request.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip, deflate, br");
// request.SendChunked = true;
// request.TransferEncoding = "gzip, deflate, br";
request.Host = "www.irctc.co.in";
//request.Headers.Add
Stream stream = request.GetRequestStream();
stream.Write(data, 0, data.Length);
stream.Close();
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
stream = response.GetResponseStream();
foreach (Cookie c in cookieJar.GetCookies(request.RequestUri))
{
MessageBox.Show("Cookie['" + c.Name + "']: " + c.Value);
}
StreamReader sr = new StreamReader(stream);
//richTextBox1.Text = sr.ReadToEnd();
String myhtml = sr.ReadToEnd().ToString();
displayHtml(myhtml);
sr.Close();
stream.Close();
}
catch (Exception ex)
{
MessageBox.Show("Error : " + ex.Message);
}
}
什麼問題?什麼是錯誤?你需要什麼幫助?在這裏簡單地發佈你的代碼不會有什麼好處。嘗試並解釋你正在做什麼以及你在做什麼時遇到的問題。幫助我們,讓我們可以幫助你。 – Hexxx