Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to parse a FORM from WebResponse into a POST body of a WebRequest
    text
    copied!<p>I'm new to this, this is my virgin voyage, the task at hand is to create a transaction in C# that will navigate through a page flow of a web app via WebRequest/WebResponse. I got the Request/Response mechanism working, cookies and all (I can successfully execute a transaction with hardcoded values for POST URLs and POST bodies), the difficulty is with generating dynamic POST body and POST URL for the WebRequest from the value pairs of WebRequest. Essentially, once the flow is started with first WebRequest, which has always the same static URL and "hardcoded" body, each following Request is built from the FORM value pairs of the previous Response, for example: part of the FORM that's in the Response (I've replaced HTML opening and closing brackets with square ones, not sure how to paste HTML straight into here):</p> <pre><code> &lt;form id="expressform" method="post" action=""&gt; &lt;div&gt; &lt;input type="hidden" name="ScreenData.widgets.modified" value=""/&gt;&lt;input type="hidden" name="ScreenData.header.hidden.name" value="ScreenData.widgets.modified"/&gt;&lt;input type="hidden" name="ScreenData.marshalled" value="true"/&gt;&lt;input type="hidden" name="ScreenData.header.hidden.name" value="ScreenData.marshalled"/&gt;&lt;input type="hidden" name="isCreateAccountWizard" value="true"/&gt;&lt;input type="hidden" name="ScreenData.header.hidden.name" value="isCreateAccountWizard"/&gt; &lt;input type="hidden" name="versionPoint" value="77777"/&gt; </code></pre> <hr> <p>and then some text areas in the form to submit values, like this:</p> <pre><code>&lt;tr&gt; &lt;td class="dataOut" style="padding-left:30px"&gt; &lt;textarea name="ScreenData.sicInfo.natureOfBusiness" rows="5" cols="60" class="dataOut" onmouseup="textAreaCounter(this,250);;" onkeypress="textAreaCounter(this,250);;" onkeyup="textAreaCounter(this,250);;" onchange="markDataDirty(this);;"&gt;&lt;/textarea&gt; &lt;/td&gt; &lt;/tr&gt; </code></pre> <p>and then on Submit there's the URL:</p> <pre><code> &lt;a class="detailBtnOn" href="javascript:submitForm('express?displayAction=CreateAccountWizard&amp;amp;saveAction=SaveCreateSICCode&amp;amp;flow=forward&amp;amp;saveActionToken=84454A7D-50FE-5856-CE17-916B70EDFE1A&amp;amp;flowToken=CF3827F4-1DE7-54B1-D87B-D72F01C454C3')"&gt;Submit&lt;/a&gt; </code></pre> <p>And then the next WebResponse should have this in its POST body:</p> <pre><code>ScreenData.widgets.modified=&amp;ScreenData.header.hidden.name=ScreenData.widgets.modified&amp;ScreenData.marshalled=true&amp;ScreenData.header.hidden.name=ScreenData.marshalled&amp;isCreateAccountWizard=true&amp;ScreenData.header.hidden.name=isCreateAccountWizard&amp;versionPoint=77777&amp;ScreenData.commonHeaderInfo.accountName=SomeAccountName&amp;ScreenData.commonHeaderInfo.effectiveDate=08%2F01%2F2011&amp;ScreenData.sicInfo.natureOfBusiness=business&amp;ScreenData.sicInfo.sic=7777&amp;ScreenData.widgets.modified=ScreenData.sicInfo.natureOfBusiness&amp;ScreenData.widgets.modified=ScreenData.sicInfo.sic </code></pre> <p>and this as a URL:</p> <pre><code>express?displayAction=CreateAccountWizard&amp;saveAction=SaveCreateSICCode&amp;flow=forward&amp;saveActionToken=84454A7D-50FE-5856-CE17-916B70EDFE1A&amp;flowToken=CF3827F4-1DE7-54B1-D87B-D72F01C454C3 </code></pre> <hr> <p>But not only I can't figure out how to build this parsing engine, I can't even grab value pairs from the FORM. I'm trying to use AgilityPack, here's a bit that should at least print out FORMs "important" content:</p> <pre><code>var page = new HtmlDocument(); page.OptionReadEncoding = false; var stream = HttpWResponse.GetResponseStream(); page.Load(stream); foreach (var f in page.DocumentNode.Descendants("form")) { foreach (var d in page.DocumentNode.Descendants("div")) { Loggers.EventsLogger.Info(""); Loggers.EventsLogger.Info((f.GetAttributeValue("name", null) ?? f.GetAttributeValue("id", "&lt;no name&gt;")) + ": "); Loggers.EventsLogger.Info(""); Loggers.EventsLogger.Info(f.GetAttributeValue("method", "&lt;no method&gt;") + ' '); Loggers.EventsLogger.Info(""); Loggers.EventsLogger.Info(f.GetAttributeValue("action", "&lt;no action&gt;")); foreach(var i in f.Descendants("input"))//{ { Loggers.EventsLogger.Info(""); Loggers.EventsLogger.Info('\t' + (i.GetAttributeValue("name", null) ?? f.GetAttributeValue("id", "&lt;no name&gt;"))); Loggers.EventsLogger.Info(""); Loggers.EventsLogger.Info(" ("); Loggers.EventsLogger.Info(""); Loggers.EventsLogger.Info(i.GetAttributeValue("type", "&lt;no type&gt;")); Loggers.EventsLogger.Info(""); Loggers.EventsLogger.Info("): " + i.GetAttributeValue("value", "&lt;no value&gt;")); } Loggers.EventsLogger.Info(""); Loggers.EventsLogger.Info(""); } } </code></pre> <hr> <p>but it only prints out this:</p> <pre><code>INFO EventsLogger - INFO EventsLogger - expressform: INFO EventsLogger - INFO EventsLogger - post </code></pre> <p>(if i get rid of the "div" bit - foreach (var d in page.DocumentNode.Descendants("div")), - nothing changes)</p> <hr> <p>Any help or suggestions on what's going on with the FORM print out parser and how to build a parsing engine for building Requests from Responses would be greatly appreciated.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload