Search code examples
javascriptc#asp.netdownloadawesomium

Automate download after ASP.NET Web Form postback using Awesomium


I have a web page written using ASP.NET Web Forms. The entirety of the page body is wrapped in a form tag which, upon submission, replies with an Excel file instead of an HTML page. The page also includes JavaScript that triggers the form submission. A normal browser reacts to this situation by asking where to save the Excel file (Awesomium does this also).

I cannot modify the page but I want to automate the download of the Excel file. I cannot use an HTTP Client because the above mentioned JavaScript computes many things that are complicated to simulate; each time the function is called, it returns a different action value for the form.

I'm currently using Awesomium.net, and have configured it to start the download. Using Fiddler I see that the download of the file starts correctly. Unfortunately, it doesn't appear to complete :(. Alternatively, the file is retrieved but nothing changes in Awesomium.net: no event is fired to inform me that the download is completed and I don't know where to retrieve this file.

I suspect it is because the returned mimetype is not text/html, but is instead application/vnd.ms-excel; my guess is that Awesomium expects an HTML file and doesn't know how to handle an Excel file. It is trying to show a "save as" modal dialog, but as it used in a headless service, no popup is available.

As a result, I don't receive any event (or, I'm looking in the wrong place) and it isn't possible to programmatically determine when the file is arrived.

Is there a way to solve this problem? Maybe intercept all the replies? I need to programmatically control the "save as" dialog and choose where to save the file.


Solution

  • i've found several more difficulties in working around this problem first: i can't modify the content-disposition header because it appears that for a reason totally unknown to me the response isn't intercepted by the fiddlerSDK that i use as proxy. anyway, fortunately, the request is intercepted. so my workaround (very dirty) was to intercept in the proxy the request made by awesomium, copy all the headers and cookies apart and abort it. Awesomium now is not expecting for any reply anymore. but i've all the information it was using for the request in my hand. i've then crafted a new HttpRequestMessage and i've used HttpClient to send it. this way i can also handle the reply better, by processing it directly in memory without to save the file to the disk and then pick it up again.

    this experience wasn't so pleasant but finally it worked. fortunately this job have to run just one a day so performance aren't critical at all, i've wished to find a better solution but i can't find any better than this. fortunately it works and works 100% of the times so i suppose i can check-in and push for tonight!