简体   繁体   中英

How do I download a CSV file to a string from an https URL?

The code that I have listed here works when I ReadAllText from a local file. What I need to be able to do is replace the path "C:\LocalFiles\myFile.csv" with " https://mySite.blah/myFile.csv ".

I have tried several methods, but can't seem to be able to get the csv file loaded into a string variable. If I could just do that, then the code would work for me.

var csv = System.IO.File.ReadAllText(@"C:\LocalFiles\myFile.csv");

StringBuilder sb = new StringBuilder();
using (var p = ChoCSVReader.LoadText(csv).WithFirstLineHeader())
{
    p.Configuration.NullValue = null;

    if (p.Configuration.CSVRecordFieldConfigurations.IsNullOrEmpty())
    {
        p.Configuration.NullValue = null;
    }

    // ChoIgnoreFieldValueMode.DBNull = ChoIgnoreFieldValueMode.Empty();
    using (var w = new ChoJSONWriter(sb))
        w.Write(p);
}
string fullJson = sb.ToString();

If I simply replace the path, I get an error message that says that the path is invalid.

You need to get the string using a web request:

string urlAddress = "https://mySite.blah/myFile.csv";

    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(urlAddress);
    HttpWebResponse response = (HttpWebResponse)request.GetResponse();

    if (response.StatusCode == HttpStatusCode.OK)
    {
        Stream receiveStream = response.GetResponseStream();
        StreamReader readStream = null;
        if (response.CharacterSet == null)
        {
            readStream = new StreamReader(receiveStream);
        }
        else
        {
            readStream = new StreamReader(receiveStream,
            Encoding.GetEncoding(response.CharacterSet));
        }

        string data = readStream.ReadToEnd();
        response.Close();
        readStream.Close();
     }

or using a Webclient:

WebClient wc = new WebClient();
string data = wc.DownloadString("https://mySite.blah/myFile.csv");

Then pass data into your reader instead of using the System.IO.File.ReadAllText(@"C:\LocalFiles\myFile.csv");

Both of the above examples assume that the file is publicly accessible at that url without authentication or specific header values.

Take a look at the WebClient class. You might want to try the DownloadFile() or DownloadString() method...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM