0

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.

jpolo
  • 35
  • 7

2 Answers2

2

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.

Stuart Frankish
  • 818
  • 1
  • 11
  • 27
  • This doesn't return a csv file as text. It comes back as a bunch of irrelevant tags between a ....

    tag.

    – jpolo Oct 15 '19 at 21:57
  • Added webclient example. – Stuart Frankish Oct 15 '19 at 21:59
  • Is it possible that I need to do something different when it's an "https" URL rather than an "http" URL? – jpolo Oct 15 '19 at 22:03
  • 1
    That depends on what exactly the "irrelevant" tags actually contain. Is the web server returning a different response than you expected? Have you read through the returned content to see if it has an error message displayed in there somewhere? – Stuart Frankish Oct 15 '19 at 22:08
  • I just need the csv text, nothing else. There are no errors. The CSV is nowhere to be found in the data variable. I would post the "irrelevant" tags, but some of them contain proprietary server names, etc. But I am expecting to see the CSV data in the "data" variable...and it's not there. – jpolo Oct 15 '19 at 22:19
  • I've double checked both mine and Peter's original methods with local demo's and both are sound methods for getting string content from the web. That you are seeing meaningful server names in your response is a sure sign that the server is spitting back your request. You'll need to sanitize your response of anything you don't want anyone to see and paste it otherwise we can't help. To your other question re HTTP(s) - Have you tried both? Some servers can be configured to only allow one or the other. – Stuart Frankish Oct 15 '19 at 22:27
  • save the string to a file and open it in a browser. I suspect you will find that this is due to your request being unauthenticated. – Sam Axe Oct 15 '19 at 23:06
  • I really appreciate everyone's help, thank you. I have tried both http and https with the same results. I suppose it is possible that the server configuration could be the issue. However, when I access the URL directly in a browser, I am able to download the csv file without an issue. I think that the answers you have given me are rock solid answers, but my issue remains :-( – jpolo Oct 16 '19 at 14:44
  • Hmm... either it's http or https. In case that it's https, perhaps an invalid ssl certificate prohibits the download. So before you use the Webclient's Downloadfile or DownloadString method you might add the following line: ```ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };``` ... that ensures that an invalid certificate prohibits the download... – Peter Schneider Oct 16 '19 at 18:25
  • It was probably an issue with the certificate, but we decided to just have the file downloaded to a local directory on the server, since it works that way. I appreciate all of the responses. Thank you. – jpolo Oct 24 '19 at 19:03
0

Replace this line

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

with

string result = client.GetStringAsync("https://mySite.blah/myFile.csv").Result;

or

var textFromFile = (new WebClient()).DownloadString("https://mySite.blah/myFile.csv");

There are many other ways to do it as well. Just google it.

Cinchoo
  • 6,088
  • 2
  • 19
  • 34