VIEWSTATE

Topics: User Forum
Aug 25, 2006 at 12:03 AM
Hi Simon,
I am loving HtmlAgilityPack. Great work!!!
Few questions for you,

1. How can I use HtmlWeb to POST to a website with ASP.NET "__VIEWSTATE" on it? It would be awesome if you can provide us with an example of HtmlWeb usage for POSTING Data!!!

2. How can I use HtmlWeb with a proxy server???

3. How can I retrieve cookies from HtmlWeb??

4. How can I enable compression while using HtmlWeb??

If you can answer a few of my above questions, it will be awesome.

Thanks so much
Indo.
Coordinator
Aug 25, 2006 at 7:11 AM
Hi

1. How can I use HtmlWeb to POST to a website with ASP.NET "__VIEWSTATE" on it? It would be awesome if you can provide us with an example of HtmlWeb usage for POSTING Data!!!

Please see this blog for some (advanced) samples:

http://blogs.wdevs.com/ultravioletconsulting/
http://blogs.wdevs.com/ultravioletconsulting/archive/2005/09/11/10517.aspx


2. How can I use HtmlWeb with a proxy server???

You can hook the HttpRequest used by the Html Agility Pack before the request is actually run:

static void SafeMain(string[] args)
{
HtmlWeb hw = new HtmlWeb();
hw.PreRequest = new HtmlAgilityPack.HtmlWeb.PreRequestHandler(OnPreRequest);
string url = @"http://www.codefluent.com";
HtmlDocument doc = hw.Load(url);
doc.Save("codefluent.htm");
}

static bool OnPreRequest(HttpWebRequest request)
{
WebProxy myProxy = new WebProxy("http://myproxy:812");
request.Proxy = myProxy;
return true; // ok, go on
}


3. How can I retrieve cookies from HtmlWeb??
Same story, but instead of using PreRequest you would use PostResponse.


4. How can I enable compression while using HtmlWeb??.
You would have to A) add the properheader on the PreRequest, to inform the server you're ok to get compressed stream, B) detect the type of content on the PostResponse, and if it's compressed, C) decompress the stream (using existing c# Deflate code you can find in libraries like SharpZipLib for example)

Simon
Aug 26, 2006 at 3:48 PM
Thank you so much Simon. Extremely helpful posting.