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
162k26 gold badges252 silver badges403 bronze badges
asked Apr 16, 2010 at 0:37
5
  • While file do you want to generate in C# code? Excel or this returned HTML from the web request? Commented 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? Commented Apr 16, 2010 at 0:40
  • Imprecise language does not survive long on SO! :) Commented Apr 16, 2010 at 0:41
  • @MusiGenesis - i meant the excel file. Commented Apr 16, 2010 at 0:42
  • 1
    Similar questions: stackoverflow.com/questions/67734/… stackoverflow.com/questions/268320/… Commented Apr 16, 2010 at 0:42

3 Answers 3

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).

answered Apr 16, 2010 at 0:45
1
  • Good Question - yes the reason is the webrequest is to a vendor product that returns response in such a manner. Commented Apr 16, 2010 at 17:48
1

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

Note that it will be slow.

answered Apr 16, 2010 at 0:39
2
  • 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 ? Commented 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! Commented Apr 16, 2010 at 0: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.

answered Apr 16, 2010 at 0:50
2
  • considered that but not much luck. the combination of fields are dynamic so will need to do runtime analysis. Commented 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. Commented Apr 17, 2010 at 10:18

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.