2

I have a webrequest that returns a html response which has form inside with hidden fields with some javascript that submits the form automatically on pageload ( if this was run in a browser).

If I save this file as *.html and run this file in browser , the java script code automatically posts the form and the output is excel file.

I want to be able to generate this file(excel) from a c# code which is not running in broswer.

I tried mocking thr form post but its complicated and has various scenarios based on the original webrequest querystring. any pointers.... i know its not possible to probably run JS code that posts the form - from within c# code but still thought of chekcing if someone has done that.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
dotnetcoder
  • 3,431
  • 9
  • 54
  • 80

3 Answers3

4

If I understand your question correctly, you have code in a web service that returns an HTML file that includes javascript which posts the form to somewhere that returns an Excel spreadsheet. Do I have this right?

When your code becomes a Rube Goldberg machine, you might want to refactor a little. Is there any reason your original web request can't be written to return an Excel spreadsheet (cutting out more than one middleman)?

Update: Sorry about the Rube Goldberg crack - I think I understand your situation now.

I would stay away from the WebBrowser for this purpose, as it's more of an amusing but dangerous toy than a reliable link in a complex machine.

Essentially, all you really need to do here is parse the HTML returned from your initial web request and extract the form values, then build your own form in code and submit it to the web service that returns an Excel spreadsheet. The javascript in the returned HTML is not actually of any importance to you, as its only purpose is to submit form values from a browser.

Html Agility Pack looks like a good choice for this (I've never used it myself, but it appears to be the most frequent answer to this type of problem on StackOverflow).

MusiGenesis
  • 74,184
  • 40
  • 190
  • 334
  • Good Question - yes the reason is the webrequest is to a vendor product that returns response in such a manner. – dotnetcoder Apr 16 '10 at 17:48
1

You can use the WebBrowser control to interact with the page in IE.

Note that it will be slow.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • my c# code is in a windows service. Do i need to open up webbrowser control in any visual form or just instance of it will work ? – dotnetcoder Apr 16 '10 at 00:40
  • If you're running in a service, this will not work reliably (but you can still try it). Good luck! – SLaks Apr 16 '10 at 00:44
1

If you know the javascript code (which you do, since you have access to the HTML) you can analyze it and find out what values are posted to the web service that returns the Excel sheet. When you know that, just use C# to post the same values, and you'll get the same sheet back. The System.Net namespace can probably help you, although I have never attempted this myself, so I don't know for sure.

Tomas Aschan
  • 58,548
  • 56
  • 243
  • 402
  • considered that but not much luck. the combination of fields are dynamic so will need to do runtime analysis. – dotnetcoder Apr 16 '10 at 01:00
  • @dotnetcoder: Still, you can probably find the parameters in the html file using a regex or something, and then do the post yourself instead of figuring out a way to run the javascript. – Tomas Aschan Apr 17 '10 at 10:18