Reputation: 3601
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.
Upvotes: 2
Views: 1934
Reputation: 75296
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).
Upvotes: 4
Reputation: 60564
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.
Upvotes: 1
Reputation: 887195
You can use the WebBrowser control to interact with the page in IE.
Note that it will be slow.
Upvotes: 1