VS.NET 2010 (and above) Users Click Here   

HOMECONTACT PRODUCTS DOWNLOADS PURCHASE TESTIMONIALS FORUMS COMPANY CONTACT
Home
Products
Downloads
Purchase
Licensing
Licensing FAQ
Software Updates
Support Forums
Testimonials
Feature Requests
Guarantee
About Us
Contact Us
Hosting Companies
Privacy Policy
   
Shopping Cart



aspNetMHT: Posting Form Data

by Dave 3. March 2011 11:25

This article will talk about posting HTML form data to a second page, so aspNetMHT can properly render that second page.

Lets say there are 2 web pages on a remote server, PageA and PageB. PageA posts Http Form data to PageB. We want to somehow convert PageB to a MHT document. The problem is, that if we navigate directly to PageB, it does not render the expected result.

How do we correctly convert PageB to a MHT document with posted data from PageA?

We need to do this in 2 steps.

1) Determine the HTML form elements that will be posted.
2) Load them in to the MHTForm object, and submit them.

Determine the HTML Form Elements
We first need to physically view the source of the rendered PageA. We need to look at all of the HTML input form elements that would normally be submitted on PageA. Some of these may look like:

<form method="post" action="PageB.aspx" >
<input type="hidden" name="__VIEWSTATE" value="dDwtMT....UcE2M2Z/lA" />
What color do you like?
<input name="txtColor" type="text" />
What size shirt?
<input name="txtSize" type="text" />
<input type="submit" value="go" name="submit">
</form>

When this form is submitted to the server, it will send along the values of txtColor and txtSize, along with the other HTML input objects. For our purposes we will specify a color of "Red" and a size of "Large".

What we want to do, is load these HTML objects into the MHTForm, so aspNetMHT can submit them to PageB for us. 

Loading the HTML Form objects into the MHTForm Object
To load the HTML form elements, the easiest way, is to simply have aspNetMHT fetch PageA and automatically find them.  Once they are loaded, we can set our own values.

For example:
 MHT m = new MHT();
 m.Form.LoadForm( "http://localhost/PageA.aspx");
 m.Form.AddFormValue("txtColor","Red");
 m.Form.AddFormValue("txtSize","Large");
 m.Form.Submit(true);
 m.SaveToFile("c:\\temp\\");

Below are complete working examples of this functionality.


C#

MHT m = new MHT();
m.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322)";

//enable logging for any troubleshooting
m.Logger = new MHTLog( "c:\\temp\\mht.log" );
m.Logger.Overwrite = true;
m.Logger.Enabled = true;

//load the form
m.Form = new MHTForm();
m.Form.LoadForm( "http://localhost/PageA.aspx");
m.Form.AddFormValue("txtColor","Red");
m.Form.AddFormValue("txtSize","Large");

//submit the form, and save the resulting MHT file
m.Form.Submit(true);
m.SaveToFile("c:\\temp\\");


VB.NET

Dim m As New MHT()
m.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322)"

'enable logging for any troubleshooting
m.Logger = New MHTLog("c:\temp\mht.log")
m.Logger.Overwrite = True
m.Logger.Enabled = True

'load the form
m.Form = New MHTForm()
m.Form.LoadForm("http://localhost/PageA.aspx")
m.Form.AddFormValue("txtColor", "Red")
m.Form.AddFormValue("txtSize", "Large")

'submit the form, and save the resulting MHT file
m.Form.Submit(True)
m.SaveToFile("c:\temp\")

As always, if anyone has any questions, feel free to contact me using the Contact Us page.

Dave
Wanta

Comments are closed

 

 

Testimonial

Thanks a lot. Your answer helped me resolve this issue, rightaway. Your support/help is outstanding. It's incredible to get your reply, even on a weekend day, so fast. You guys are setting a standard that few people can match. I wish you the very best! "

Tiru | Magnaquest

Read more testimonials
ListNanny aspNetDNS aspNetEmail aspNetPOP3 aspNetMX IPMuncher aspNetMIME aspNetPING aspNetTraceRoute aspNetIMAP aspNetMHT