Additional Login methods

Topics: Developer Forum, User Forum
May 5, 2007 at 6:27 PM
For people who want to screen scrape a web page but first must login here are two extra methods to HtmlWeb that will accomplish this. Also check out John's blog for more on how to login -- http://aspzone.com/blogs/john/archive/2006/05/11/1778.aspx

public HtmlDocument Login( string loginUri, string loginData, string requestUri )
{
try
{
// Reference: http://aspzone.com/blogs/john/archive/2006/05/11/1778.aspx

// cookieContainer is used to store the cookies used by the login
CookieContainer cookieContainer = new CookieContainer();

// First hit the login page
HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create( loginUri );
req.CookieContainer = cookieContainer;
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
byte[] loginDataBytes = encoding.GetBytes( loginData );
req.ContentLength = loginDataBytes.Length;
Stream stream = req.GetRequestStream();
stream.Write( loginDataBytes, 0, loginDataBytes.Length );
stream.Close();

HttpWebResponse res = (HttpWebResponse)req.GetResponse();
// Then grab the content of the desired page
req = (HttpWebRequest)HttpWebRequest.Create( requestUri );
req.CookieContainer = cookieContainer;
req.Method = "GET";
res = (HttpWebResponse)req.GetResponse();


System.Text.Encoding respenc;

if ( ( res.ContentEncoding != null ) && ( res.ContentEncoding.Length > 0 ) )
{
respenc = System.Text.Encoding.GetEncoding( res.ContentEncoding );
}
else
{
respenc = null;
}

HtmlDocument doc = new HtmlDocument();
Stream s = res.GetResponseStream();
if ( respenc != null )
{
doc.Load( s, respenc );
}
else
{
doc.Load( s, true );
}
res.Close();

return doc;
}
catch
{
return new HtmlDocument();
}
}

public void LoadHtmlAsXml( HtmlDocument doc, string xsltUrl, XmlTextWriter writer )
{
XslCompiledTransform xslt = new XslCompiledTransform();
xslt.Load( xsltUrl );
xslt.Transform( doc, writer );
}
Jul 30, 2007 at 1:35 PM
hi
http://aspzone.com/blogs/john/archive/2006/05/11/1778.aspx
is not running please refer me
thanks