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.
-
While file do you want to generate in C# code? Excel or this returned HTML from the web request?TheGeekYouNeed– TheGeekYouNeed2010年04月16日 00:40:08 +00:00Commented Apr 16, 2010 at 0:40
-
When you say "I want to be able to generate this file ...", do you mean the HTML file or the Excel file?MusiGenesis– MusiGenesis2010年04月16日 00:40:33 +00:00Commented Apr 16, 2010 at 0:40
-
Imprecise language does not survive long on SO! :)MusiGenesis– MusiGenesis2010年04月16日 00:41:08 +00:00Commented Apr 16, 2010 at 0:41
-
@MusiGenesis - i meant the excel file.dotnetcoder– dotnetcoder2010年04月16日 00:42:27 +00:00Commented Apr 16, 2010 at 0:42
-
1Similar questions: stackoverflow.com/questions/67734/… stackoverflow.com/questions/268320/…Sasha Chedygov– Sasha Chedygov2010年04月16日 00:42:57 +00:00Commented Apr 16, 2010 at 0:42
3 Answers 3
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).
-
Good Question - yes the reason is the webrequest is to a vendor product that returns response in such a manner.dotnetcoder– dotnetcoder2010年04月16日 17:48:50 +00:00Commented Apr 16, 2010 at 17:48
You can use the WebBrowser control to interact with the page in IE.
Note that it will be slow.
-
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– dotnetcoder2010年04月16日 00:40:38 +00:00Commented Apr 16, 2010 at 0:40
-
If you're running in a service, this will not work reliably (but you can still try it). Good luck!SLaks– SLaks2010年04月16日 00:44:48 +00:00Commented Apr 16, 2010 at 0:44
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.
-
considered that but not much luck. the combination of fields are dynamic so will need to do runtime analysis.dotnetcoder– dotnetcoder2010年04月16日 01:00:38 +00:00Commented Apr 16, 2010 at 1: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– Tomas Aschan2010年04月17日 10:18:03 +00:00Commented Apr 17, 2010 at 10:18