Now let me show you how to use HttpWebRequest to pass through GSA login form. After that, you can extract the web page content by yourself.
The GSA (Google Search Appliance) login form has a special authentication framework to pervent the hacking. It is based on assigning several cookies to client browser during HTTP connecting session. Now let me show you how to use HttpWebRequest to pass through GSA login form. After that, you can extract the web page content by yourself.
First, you need to maintain a CookieContainer object during the whole process:
CookieContainer cc = new CookieContainer();
Then, we need to create a byte array to store the login data, please replace [User Name] and [password] in string loginData below to your case in GSA:
string loginData = "actionType=authenticateUser&login=Login&userName=[User Name]&password=[Password]";
ASCIIEncoding asciiEnc = new ASCIIEncoding();
byte[] byteArray = asciiEnc.GetBytes(loginData);
process 3 times http request to pass through GSA login form, please replace [GSA URL] to your case:
// first time
HttpWebRequest req = HttpWebRequest.Create("http://[GSA URL]/") as HttpWebRequest;
req.CookieContainer = cc;
req.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)";
HttpWebResponse res = req.GetResponse() as HttpWebResponse;
// second time
req = HttpWebRequest.Create("http://[GSA URL]/EnterpriseController") as HttpWebRequest;
req.CookieContainer = cc;
req.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)";
res = req.GetResponse() as HttpWebResponse;
// third time
req = HttpWebRequest.Create("http://[GSA URL]/EnterpriseController") as HttpWebRequest;
req.CookieContainer = cc;
req.Referer = http://[GSA URL]/EnterpriseController;
req.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)";
req.ContentType = "application/x-www-form-urlencoded";
req.Method = "POST";
req.ContentLength = byteArray.Length;
Stream s = req.GetRequestStream();
s.Write(byteArray, 0, byteArray.Length);
s.Close();
res = req.GetResponse() as HttpWebResponse;
OK, now we can pass parameters to get any web page content, please replace [Parameters] from code snippet below to your customized one:
req = HttpWebRequest.Create(http://[GSA URL]/EnterpriseController/?actionType=[Parameters]") as HttpWebRequest;
req.CookieContainer = cc;
req.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)";
res = req.GetResponse() as HttpWebResponse;
s = res.GetResponseStream();
StreamReader sr = new StreamReader(s);
string result = sr.ReadToEnd();
sr.Close();
s.Close();
The result string is the final web page content.
[re-post this article due to database crash.]
沒有留言:
張貼留言