Could you help me with this question, I have a windows form in VS2019, which I want to get the html generated after send the captcha and other parameter (2 parameters), my windows form contains devexpress controls, a picture edit where I load the captcha, textedit1 where I put the captcha, textedit2 where i put the second parameter (e.g. 06892898), simplebutton1 where I clic to load the captcha to the picture edit, the simplebutton2 to request the website with the 2 parameters, the problem is that when i passed the two parameter in the url, so I got an html with the message "The session has finished", How could I get the session or keep alive the webclient object in C#. Thanks in advance, This is my code:
private WebClient myClient = new WebClient();
public FrmData()
{
InitializeComponent();
}
private void btnCaptcha_Click(object sender, EventArgs e)
{
myClient.DownloadFile(new Uri("http://ww4.essalud.gob.pe:7777/acredita/captcha.jpg"), @"c:\temp\captcha.jpg");
pictureCaptcha.Image = Image.FromFile(@"c:\temp\captcha.jpg");
}
private void btnLoad_Click(object sender, EventArgs e)
{
string line = string.Empty;
try
{
HttpWebRequest req = (HttpWebRequest)WebRequest.Create("http://ww4.essalud.gob.pe:7777/acredita/servlet/Ctrlwacre?captchafield_doc=" + txtedit1.Text + "&td=1&nd=" + txtedit2.Text);
HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
StreamReader sr = new StreamReader(resp.GetResponseStream(), System.Text.Encoding.UTF8);
line = sr.ReadToEnd();
sr.Close();
resp.Close();
XtraMessageBox.Show("" + line);
}
catch (Exception ex)
{
XtraMessageBox.Show("" + ex.Message);
}
}
I got the solution using HttpWebRequest, HttpWebResponse, CookieContainer class, I did this code:
private HttpWebRequest request;
private HttpWebResponse response;
private CookieContainer cookies = new CookieContainer();
private void btnCaptcha_Click(object sender, EventArgs e)
{
try
{
request = (HttpWebRequest)WebRequest.Create("http://ww4.essalud.gob.pe:7777/acredita/captcha.jpg");
request.CookieContainer = cookies;
request.Timeout = 10000;
request.ReadWriteTimeout = 10000;
request.KeepAlive = true;
response = (HttpWebResponse)request.GetResponse();
pictureCaptcha.Image = Image.FromStream(response.GetResponseStream());
}
catch (Exception ex)
{
XtraMessageBox.Show("" + ex.Message);
}
}
private void btnLoad_Click(object sender, EventArgs e)
{
string line = string.Empty;
try
{
request = (HttpWebRequest)WebRequest.Create("http://ww4.essalud.gob.pe:7777/acredita/servlet/Ctrlwacre?captchafield_doc=" + txtCaptcha.Text + "&td=1&nd=06915398");
request.CookieContainer = cookies;
response = (HttpWebResponse)request.GetResponse();
StreamReader sr = new StreamReader(response.GetResponseStream(), System.Text.Encoding.UTF8);
line = sr.ReadToEnd();
sr.Close();
response.Close();
}
catch (Exception ex)
{
XtraMessageBox.Show("" + ex.Message);
}
XtraMessageBox.Show("" + line);
}
Thanks for all the comments and the idea to use cookies and keep alive.