How to use Asp.net 1.1 webrequest like java apache httpclient? - c#

I am new in asp.net and I have asp.net 1.1 in my server.
I want to do in background --> login to another site and navigate in it <-- to retrieve some information needed to process in my page.
With Apache httpclient I can maintain the login session at background and navigate how much time I need and get elements by id, class, name (td,tr,etc) with Jsoup (a parser)
Until now I can just retrieve the whole code from the other page with that, but when I try to go to other page using CookieContainer it still doesn't work:
I did this example to show what I trying to do..
protected void Page_Load(object sender, EventArgs e)
{
CookieContainer cookieJar = new CookieContainer();
HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create("http://www.4shared.com/web/login/validate");
req.Method = "POST";
req.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
req.Headers.Add("Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7");
req.KeepAlive = true;
req.Headers.Add("Keep-Alive: 300");
req.AllowAutoRedirect = false;
req.ContentType = "application/x-www-form-urlencoded";
req.CookieContainer = cookieJar;
String Username = "user%40email.com";
String PassWord = "password";
StreamWriter sw = new StreamWriter(req.GetRequestStream());
sw.Write("login=" + Username + "&password=" + PassWord);
sw.Close();
HttpWebResponse response = (HttpWebResponse)req.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding(1255));
string tmp = reader.ReadToEnd();
HttpWebRequest req2 = (HttpWebRequest)HttpWebRequest.Create("http://www.4shared.com/account/home.jsp");
req2.Method = "GET";
req2.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
req2.Headers.Add("Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7");
req2.KeepAlive = true;
req2.Headers.Add("Keep-Alive: 300");
req2.CookieContainer = cookieJar;
req2.ContentType = "text/html";
HttpWebResponse response2 = (HttpWebResponse)req2.GetResponse();
StreamReader reader2 = new StreamReader(response2.GetResponseStream(), Encoding.GetEncoding(1255));
string tmp2 = reader2.ReadToEnd();
response2.Close();
Panel2.Text = tmp2;
}
I need to retrieve the page login and show a captcha, and then POST with login info, and after that navigate to get some necessary to process in my page.
What I can't do is navigate to another page. And before that get some elements from response.

Related

Can I use HttpWebRequest to retrieve page content which have a lot based on Ajax in it?

Trying to fetch data from a URL and then parse the sensitive content.
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URL);
request.UseDefaultCredentials = true;
request.PreAuthenticate = true;
request.CookieContainer = new CookieContainer();
request.AllowAutoRedirect = true;
request.Credentials = CredentialCache.DefaultCredentials;
HttpWebResponse resp = request.GetResponse() as HttpWebResponse;
StreamReader reader = new StreamReader(resp.GetResponseStream());
var data = reader.ReadToEnd();
If check the data, not everything got returned. How can I fetch the dynamic parts which I can see on the page like Chrome Dev tool does from browser.

C# How can I call multiple POST, GET requests with same cookie via HttpWebRequest?

my question is, how can I call multiple requests via HttpWebRequest with same authenticate cookie in C#? I tried a lot of times but for now I dunno how to do it :/
My code is below:
var postData = "method=loginFormAccount&args[0][email]=###&args[0][pass]=###&args[0][cache]=37317&args[]=1";
var data = Encoding.ASCII.GetBytes(postData);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("###");
request.CookieContainer = new CookieContainer();
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.AllowAutoRedirect = true;
using (var stream = request.GetRequestStream())
{
stream.Write(data, 0, data.Length);
}
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
var cookies = new CookieContainer();
cookies.Add(response.Cookies);
System.IO.File.WriteAllText(#desktop + "\\post.html", new StreamReader(response.GetResponseStream()).ReadToEnd());
// =================================== END LOGIN ==================================== \\
System.IO.File.WriteAllText(#desktop + "\\cookie.html","");
foreach (Cookie cook in response.Cookies)
{
using (System.IO.StreamWriter file = new System.IO.StreamWriter(#desktop + "\\cookie.html", true))
{
file.WriteLine(cook.ToString());
}
// Show the string representation of the cookie.
}
HttpWebRequest requestNext = (HttpWebRequest)WebRequest.Create("####");
requestNext.CookieContainer = cookies;
requestNext.Method = "GET";
HttpWebResponse responseNext = (HttpWebResponse)requestNext.GetResponse();
//var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
System.IO.File.WriteAllText(#desktop + "\\get.html", new StreamReader(responseNext.GetResponseStream()).ReadToEnd());
My main problem is that, cookie which I'm getting is the cookie BEFORE authenticate so I must to do something to get cookie AFTER authenticate.
Try this :
HttpWebRequest requestNext = (HttpWebRequest)WebRequest.Create("####");
requestNext.CookieContainer.Add(cookies);

Login to site using watin - c#

The test site I have displays a pop up box to enter user credentials. I can log in to that site using
private CredentialCache GetCredential(LoginData loginData)
{
ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;
CredentialCache credentialCache = new CredentialCache();
credentialCache.Add(new System.Uri(loginData.LoginURL), "Basic", new NetworkCredential(loginData.LoginUserName, loginData.LoginPwd));
return credentialCache;
}
//above code is thanks to another post on SO
public void SiteLogin(LoginData loginData)
{
CookieContainer cookieContainer = new CookieContainer();
HttpWebRequest request = WebRequest.Create(loginData.LoginURL) as HttpWebRequest;
request.CookieContainer = cookieContainer;
request.Method = WebRequestMethods.Http.Post;
request.KeepAlive = false;
request.ContentLength = 0;
request.Credentials = GetCredential(loginData);
request.PreAuthenticate = true;
HttpWebResponse response = request.GetResponse() as HttpWebResponse;
System.IO.Stream responseStream = response.GetResponseStream();
System.IO.StreamReader reader = new System.IO.StreamReader(responseStream, Encoding.UTF8);
string testString = reader.ReadToEnd(); //see what response is here, only for initial coding
}
I do not have access to the test web site so I do not know what the names of the controls are for the login pop up.
How do i perform similar step with watin?

"Cookies are disabled" on Login with Webclient

I am trying to login to a Website using the following Code
CookieContainer cookieContainer = new CookieContainer();
string formUrl = "https://dualis.dhbw.de/scripts/mgrqcgi";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(formUrl);
request.Method = "POST";
request.CookieContainer = cookieContainer;
request.Referer = "https://dualis.dhbw.de";
request.ContentType = "application/x-www-form-urlencoded";
using (StreamWriter writer = new StreamWriter(request.GetRequestStream(), Encoding.ASCII)) {
writer.Write("usrname=" + username + "&pass=" + password);
}
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) {
using (StreamReader reader = new StreamReader(response.GetResponseStream())) {
String result = reader.ReadToEnd();
}
}
But the Website says something like "Your Browsers cookies are disabled…"
Thanks for your help.
The Website sets a Cookie on a previous Page.
After getting that Page and the Cookie the Login works.

C# Multiple http requests same PHP session

I'm trying to get the picture to load in the same PHP session in which the POST request will be sent.
But because i'm using button1_Click this is not possible.
And the outcome is to get the picture to load before the data is sent.
If you got any questions please ask.
i know i go wrong with the picture loading, but i dont know exactly where..
using visual c# 2010 express winforms
private void Form1_Load(object sender, EventArgs e)
{
pictureBox1.ImageLocation = "http://localhost/proj/guess-my-fav/1.jpg";
}
private void button1_Click(object sender, EventArgs e)
{
Uri uri = new Uri("http://localhost/proj/guess-my-fav/level14.php");
var answer = textBox1.Text;
string data = "guess=" + answer + "&level=14&time=opt";
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(uri);
request.Method = WebRequestMethods.Http.Post;
request.CookieContainer = new CookieContainer();
request.KeepAlive = true;
request.ContentLength = data.Length;
request.ContentType = "application/x-www-form-urlencoded";
StreamWriter writer = new StreamWriter(request.GetRequestStream());
writer.Write(data);
writer.Close();
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream());
string tmp = reader.ReadToEnd();
response.Close();
richTextBox1.AppendText(tmp); // log - delete this line
}
How can i put the rendering of image under the second request?
pictureBox1.ImageLocation = "http://localhost/proj/guess-my-fav/1.jpg";
This is going to cause the client's browser to make a request for 1.jpg
Uri uri = new Uri("http://localhost/proj/guess-my-fav/level14.php");
...
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
This is going to cause the webserver running the ASP.NET website to make a request for level14.php
You're not going to get those two requests using the same session since they'll be coming from two different machines!
You might like to look into moving that HttpWebRequest code out of the back end, and reimplementing it on the client side as an AJAX request. Then both requests will be coming from the client's browser.
If you modify your code to match
private CookieContainer cookieContainer;
private void Form1_Load(object sender, EventArgs e)
{
var wr = (HttpWebRequest)WebRequest.Create("http://localhost/proj/guess-my-fav/1.jpg");
cookieContainer = new CookieContainer();
wr.CookieContainer = this.cookieContainer;
var resp = (HttpWebResponse)wr.GetResponse();
wr.CookieContainer = cookieContainer;
using (var s = resp.GetResponseStream())
{
pictureBox1.Image = new Bitmap(s);
}
}
private void button1_Click(object sender, EventArgs e)
{
Uri uri = new Uri("http://localhost/proj/guess-my-fav/level14.php");
var answer = textBox1.Text;
string data = "guess=" + answer + "&level=14&time=opt";
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(uri);
request.Method = WebRequestMethods.Http.Post;
request.CookieContainer = cookieContainer;
request.KeepAlive = true;
request.ContentLength = data.Length;
request.ContentType = "application/x-www-form-urlencoded";
StreamWriter writer = new StreamWriter(request.GetRequestStream());
writer.Write(data);
writer.Close();
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream());
string tmp = reader.ReadToEnd();
response.Close();
richTextBox1.AppendText(tmp); // log - delete this line
}
This should make both requests to the server using the same session.
Hope this helps.
I am assuming here that you want to re-load the image when you click the button.
First of all that ImageLocation property is probably not gonna respect your session cookie, so you might have to download the image manually. You already used a CookieContainer so that's a good start.
What we want to do here is use a new HttpWebRequest to download the image and attach the same CookieContainer to it as this one should hold the session id after your first call.
We can then use the HttpWebResponse to create an Image object and assign this to the pictureBox1.Image property.
All this together might look something like this:
private void button1_Click(object sender, EventArgs e)
{
Uri uri = new Uri("http://localhost/proj/guess-my-fav/level14.php");
var answer = textBox1.Text;
string data = "guess=" + answer + "&level=14&time=opt";
CookieContainer cookies = new CookieContainer(); /* we want to have this for other call also
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(uri);
request.Method = WebRequestMethods.Http.Post;
request.CookieContainer = cookies;
request.KeepAlive = true;
request.ContentLength = data.Length;
request.ContentType = "application/x-www-form-urlencoded";
StreamWriter writer = new StreamWriter(request.GetRequestStream());
writer.Write(data);
writer.Close();
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream());
string tmp = reader.ReadToEnd();
response.Close();
richTextBox1.AppendText(tmp); // log - delete this line
HttpWebRequest request2 = (HttpWebRequest)HttpWebRequest.Create("http://localhost/proj/guess-my-fav/1.jpg");
request2.CookieContainer = cookies;
HttpWebResponse response2 = (HttpWebResponse)request.GetResponse();
pictureBox1.Image = Image.LoadFromStream(response2.GetResponseStream());
}

Categories

Resources