load web page and click to download file from console application - c#

I need to use C# Console Application.
I have a web Page URL
Inside the web page there is a button that downloads file by click
In View Source of the web page ,the button looks like this:
<a class="A" href="/myfiles/myfile.csv" target="_blank">הורד קובץ</a>
I should get the csv file in code by the URL.
How can I do it?
my question is similiar to this one , but not the same load a web page and click a button using c# I didnt find an answer yet.

Use WebClient class to download the HTML. Parse it manually to pick out the href. Use the WebClient class again to make a call to the href target. This should cause the file content to be downloaded as a response to your HTTP request.

Related

C# windows service: HTML page with picture, CSS and JS as a response

I am learning to build a windows service, but it takes requests (e.g. localhost:8081/index) from the browser to. Therefore, the HTTP response should contain an HTML page.
The HTML page looks okay when I double click the index.html file, but it lost all the CSS and js files when I request from the web browser. And I open the developer's tool in chrome and found out that all the CSS and JS files were corrupted and contain the code from my HTML page (weird).
I used HttpListenerContext class to listen for http://localhost/index request, and then open index.html file and used File.ReadAllBytes(file). When composing the response, I used the following code:
responseBytes = File.ReadAllBytes(file);
response.ContentLength64 = responseBytes.Length;
await response.OutputStream.WriteAsync(responseBytes, 0, responseBytes.Length);
response.OutputStream.Close();
Can anyone help me to figure out why this is happening?
So I figured out the answer by myself when I traced the code. When request localhost/index comes in from the browser, the GET method will first acquire index.html first, then there are other GET requests come in for the CSS, image and JS as well (It's weird that I didn't see the later requests before.)
Then I updated the handler to compose responses contains CSS, image and JS files. Everything works perfectly now.

Unable to download file (page url same as download url)

I'm trying to download a zip file (that is normally accessed/downloaded by pressing a button on a web page) using C#.
Normally the file is downloaded by selecting "Data Export" and then clicking the "SEARCH" button at this URL:
http://insynsok.fi.se/SearchPage.aspx?reporttype=0&culture=en-GB&fromdate=2016-05-30&tomdate=2016-06-03
If trigger the download manually on the webpage and then copy the download url from the 'Downloads' view of chrome or firefox I get the exact same URL as above. When I paste that in a browser window I will not trigger the download, instead the above page will be loaded and I have to trigger the download manually in the same way as in the first place.
I've also tried using the network tab of the inspector to copy the request header of the request that is triggered when clicking the "SEARCH" button, but that URL is also the same as the one above.
Trying with C# I get the same result, the page itself is downloaded. My code looks as follows:
using (var client = new WebClient())
{
client.DownloadFile("http://insynsok.fi.se/SearchPage.aspx?reporttype=0&culture=sv-SE&fromdate=2016-05-30&tomdate=2016-06-03", "zipfile.zip");
}
My guess is that my code is correct, but how do I get the correct URL to be able to download the file directly?
ASP.net inserts a bunch of crap into the the page to make things like this particularly hard. (Validation tokens, form tokens, etc).
Your best bet is to use a python library called Mechanize, or if you want to stick to C# you can use Selenium or C# WebBrowser. This will fully automate visiting the page (you can render the C# WebBrowser invisible), then just click the button to trigger the download programatically.

Redirect to .pdf

On browsing local.com/test/document, I want to redirect to local.com/document/demo.pdf
It is getting redirected properly. i.e demo.pdf file is getting downloaded. But browser Url is not getting changed.
I am using following lines of code for redirection:
HttpContext.Current.Response.Redirect("local.com/document/demo.pdf", false);
context.ApplicationInstance.CompleteRequest();
Is this the correct behavior as we are redirecting to file? or is there a way where file can be downloaded and also browser url gets changed?
It is not possible using Response.Redirect(),because the it's a function and can do one task at a time.
You can use
<a href="http://xyz.pdf" **target="_self"**>Click Me</a>
Change the target _blank,it will redirect to a new tab.
You can also go with
<iframe src="xyz.pdf"></iframe>

Download a File using webrequest.create c# via redirected link

I am trying to download a file from a page that redirects itself using JS.
For example: https://wix.codeplex.com/downloads/get/119160
When that page is loaded in the browser the file download starts automatically using a JS function.
But when I try to download it using webrequest.create I only get the HTML of that page (119160).
Is there any clean way to download the file without inspecting the html/js manually?
Thanks,
Could use WebBrowser control (which can handle JS execution) then subscribe to the FileDownload event.
For a little earlier notice, you could subscribe to Navigating (which should get triggered once the JS kicks in).

Use HTML string from Server Requets, and create the web page without saving it a file [in C#]

I´m sending the value of a variable via POST to a PHP page in C#. I get the data stream from the server that has all the web page in HTML with the value of the POST. This information is stored in a string variable.
I would like to open a browser and show the web page (maybe using System.Diagnostics.Process.Start("URL")), without having to save it in a file, this is showing the page in the moment and, when the browser is closed, no file is stored in the server.
Any idea?
Drop a WebBrowser control into a new form webBrowser1 and set its DocumentTextProperty to your result html
webBrowser1.DocumentText = ("<html><body>hello world</body></html>");
source:
<html><body>hello world</body></html>
You aren't going to be able to do that in an agnostic way.
If you simply wanted to open the URL in a browser, then using the Process class would work.
Unfortunately, in your case, you already have the content from creating the POST to the server, and you really want to stream that response in your application to the browser.
It's possible among the some browsers, but it's not able to be done in an agnostic way (and it's complicated even when targeting a specific browser).
To complicate matters, you want the browser to believe that the stream you are sending it is really coming from the server, when in reality, it's not.
I believe that your best bet would be to save the response to the file system in a temp file. However, before you do, add the <base> tag to the file with the URL that the file came from. This way, relative URLs will resolve correctly when rendered in the browser.
Then, just open the temporary file in the browser using the Process class.

Categories