0
using (var client = new WebClient())
{            
    client.UseDefaultCredentials = true;
    client.Credentials = new NetworkCredential("abcd","abcd1234");
    string downloadlink_INC="https://completeurl.com/";
    client.DownloadFile(downloadlink_INC, "downladed.csv");                
}

Trying to download a .csv file from a logged in web account which uses windows authentication. But here I have given a default network credentials to allow those users as well which don't have access to the website. The link I am passing is downloading the .csv file from the browser in the download folder, but when the same(https://completeurl.com/) is being used via C# code the content is HTML code not the columns.

Through c# the file is nothing but the html code of the page.

Thanks in advance!!

The CSV header view

coder9090
  • 63
  • 9
  • welcome to stackoverflow. the question is not very clear, can you please make description a bit more clear – Arsen Mkrtchyan Sep 01 '16 at 11:36
  • How do you want to receive the content? How does it look like normally when you do not do it in C#? – rbr94 Sep 01 '16 at 11:36
  • Hi Arsen, I have a website which uses account credentials to login(like Windows credentials), so you do not have to login the site every time. just login to your system and hit the site, it will show your name on top. So I have a link which if pasted to address bar of the same browser will download a csv file. In this case the content is good and in tabular form. But when I run the same url via the above code it downloads the csv file but the content is complete HTLM code in one column. – coder9090 Sep 01 '16 at 13:54
  • Hi Robin, the content is simply table form, rows and columns. its like you are seeing some records on a webpage in table form and you want to export it to CSV file. The CSV file will be downloaded with same data which you are seeing. – coder9090 Sep 01 '16 at 14:01

1 Answers1

0

See the answer to a similar question: Domain credentials for a WebClient class don't work

Using the answer mentioned, the following code worked for me when trying to access a resource behind a web site protected with Windows Authentication:

using (var client = new WebClient())
{
    string username = "";
    string password = "";
    string domain = "";
    string downloadlink_INC = "https://completeurl.com/";

    client.UseDefaultCredentials = false;
    CredentialCache cc = new CredentialCache();
    cc.Add(
        new Uri("https://completeurl.com/"),
        "NTLM",
        new NetworkCredential(username, password, domain));
    client.Credentials = cc;

    client.DownloadFile(downloadlink_INC, "downladed.csv");
}

However, I would discourage you from storing Windows login credentials in your source code and distributing the binary, as the binary can be reverse-engineered and your credentials exposed.

Community
  • 1
  • 1
sly
  • 300
  • 1
  • 5
  • Thanks sly, but as I mentioned I am not trying to download the resource of the page. I am trying to download a table/report or say trying to export to csv file and the content will be in tabular format. I have attached the screenshot of the csv file header in the question. – coder9090 Sep 01 '16 at 19:13
  • @Ashish If you're trying to convert HTML tables to a CSV file, take a look at [Parsing HTML table to C# usable datalist](http://blog.ditran.net/parsing-html-table-to-c-usable-datalist/) and [Loop thorough multiple HTML tables in HTML Agility Pack](http://stackoverflow.com/questions/39373749/loop-thorough-multiple-html-tables-in-html-agility-pack). Once the html table data is in a DataSet or DataTable it would be simple to export to CSV. – sly Sep 07 '16 at 17:53
  • Thanks Sly, as an HTML table will always be a static content table which we have put on the HTML body of the Webpage. But the table which I am trying to Download is changing daily. On daily basis the table shows the new entries of yesterday. If I am downloading the content today 09/13/2016, the content of the table will show me records which are new and inserted yesterday. Once they are loaded in the table and show up on the page, then from the Table header right clicking will give me option to Export the content to CSV/PDF/XLS which will download a file in my local folder. – coder9090 Sep 13 '16 at 06:38