Click here to Skip to main content
15,885,435 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi Friends,

To read data from our client API,first I am login into the url https://site.com by using username,password and location.

After login to get the data another Wep APi url is there https://site.com/users/reports/csv?location_id=42&from_date=2020-11-02&to_date=2020-11-05

After login we used this url the file is automatically downloading in the browser.

I want to read this csv file content and I need to insert the records into the database.
how to do this?

What I have tried:

I have tried the following code to login after that I am using the downloaded url.
var userName = "tec@gmail.com";
            var passwd = "5CadcPT";
            var url = "https://site.com";
            var location= "Hyderabad";
         
             var client = new HttpClient();

            var authToken = Encoding.ASCII.GetBytes($"{userName}:{passwd}:{location}");

            client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic",
                    Convert.ToBase64String(authToken));

            var result1 = await client.GetAsync(url);

            var content = await result1.Content.ReadAsStringAsync();

and I used
stream = await response.Content.ReadAsStreamAsync();

var fileInfo = new FileInfo("reports_2020-11-10 08_14_12.csv");
using (var fileStream = fileInfo.OpenWrite())
{
    await stream.CopyToAsync(fileStream);
}

File is downloading but the html content is coming in the csv file
Posted
Comments
F-ES Sitecore 18-Nov-20 6:06am    
That will only work if the site uses basic authentication which is fairly unlikely.
BillWoodruff 18-Nov-20 7:21am    
"the html content is coming in the csv file"

do you mean you are getting an html table that DOES have the data you want in it ?
prasanna204 18-Nov-20 8:31am    
no Billwoodruff.If I logged into the site through web browser it is downloading csv file with proper data.When I am implimenting this in the code instead of coming proper data it is displaying html content like a page view Source in the browser.
[no name] 18-Nov-20 12:58pm    
You're looking for "database insert code"?
prasanna204 19-Nov-20 9:40am    
no I need to read download csv file from the web Api.When I am testing from the browser means first I login into the site and I will enter csv file API URl file is downloading.While implementing it in the code.Html content is coming in the csv file.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900