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 Web 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.
for web Api login I wrote this code
- var userName = "[email protected]";
- 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();
- For downloading file I used this code file is downloading but the HTML content is coming in the downloaded CSV file.
- 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);
- }
how to do this?