Login and retrieve data from embedded form page us

2019-06-03 13:38发布

问题:

I'm a bit confused on how to go about this as I'm not really conversant with web stuff. I'm using a console application in C# to try and retrieve value from a page link inside a password protected homepage. I'm using the following details

Here's the code I'm trying:

HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create("");
req.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.0.3705;)";
req.Method = "POST";
req.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
req.Headers.Add("Accept-Language: en-us,en;q=0.5");
req.Headers.Add("Accept-Encoding: gzip,deflate");
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.Referer = "copy from url";

req.ContentType = "application/x-www-form-urlencoded";

String Username = copy from url;
String PassWord = copy from url;

StreamWriter sw = new StreamWriter(req.GetRequestStream());
sw.Write(string.Format("&loginname={0}&password={1}&btnSubmit=Log In&institutioncode=H4V9KLUT45AV&version=2", Username, PassWord));
sw.Close();
HttpWebResponse response = (HttpWebResponse)req.GetResponse();

StreamReader reader = new StreamReader(response.GetResponseStream());
string tmp = reader.ReadToEnd();

However, when I inspect the data retrieved from the web page it shows something like this:

'...Your Session has timed out due to inactivity.Please logout and relogin.
return to login page>'

I'm guessing this is due to some VIEWSTATE stuff in ASP.NET I'm also guessing I might have a problem with retrieving the data from the link I'll extract from the homepage, coz it seems the link simply loads data into a frame rather than reload the webpage.

Anyone please?

回答1:

Your form data is incorrect. After removing the & at the beginning it worked for me:

sw.Write(string.Format("loginname={0}&password={1}&btnSubmit=Log In&institutioncode=H4V9KLUT45AV&version=2", Username, PassWord));

Additionally, as already mentioned in the other answer, you need to add the returned ASPSESSIONIDSSRRDRST cookie in further requests to the site.



回答2:

Ok... the website is using Cookies, so, after you logged in you need to retrieve the cookies first, to make another WebRequest:

CookieCollection cookiesResponse = new CookieCollection();

if (response != null)
{
    foreach (string cookie in response.Headers["Set-Cookie"].Split(';'))
    {
        string name = cookie.Split('=')[0];
        string value = cookie.Substring(name.Length + 1);
        cookiesResponse.Add(new Cookie(name.Trim(), value.Trim(), path, domain));
    }
}

In you example the cookie contains: ASPSESSIONIDSSRRDRST=FEKODBMDBEIPCLLENCFLFBEA

You must use that CookieCollection for any request to the web, in your request you can set the cookies:

request.CookieContainer = cookiesResponse;

And finaly, you can parse the response. You can use an html tag parse, or parse the plain text.

I hope this is helpful.