-1

I'm trying to download a csv file from the following link but an exception "illegal characters in path error" is thrown at the last line. I believe it's the question mark sign in the link that messes up everything but I have to get it working.. Any suggestions?

string remoteUri = "download.finance.yahoo.com/d/quotes.csv?s=%40%5EDJI,aapl&f=o&e=.csv";
string fileName = "aapl.csv", myStringWebResource = null;
// Create a new WebClient instance.
WebClient myWebClient = new WebClient();
// Concatenate the domain with the Web resource filename.
myStringWebResource = remoteUri + fileName;
// Download the Web resource and save it into the current filesystem folder.
myWebClient.DownloadFile(myStringWebResource, fileName);
aloisdg
  • 22,270
  • 6
  • 85
  • 105
Ofer Gozlan
  • 953
  • 2
  • 9
  • 21
  • You could could just replace the question mark with another character... – rory.ap Sep 25 '15 at 15:08
  • 2
    Question marks are valid url characters. – Dave Zych Sep 25 '15 at 15:09
  • This link could be helpful : http://stackoverflow.com/questions/146134/how-to-remove-illegal-characters-from-path-and-filenames – Ruchi Sep 25 '15 at 15:09
  • 1
    Why doesn't your uri start with protocol? – Sergei Rogovtcev Sep 25 '15 at 15:09
  • Cannot reproduce. The second argument for `DownloadFile()` is the path to download the file to, and that contains "aapl.csv" in your example. That path will not throw the reported exception. – CodeCaster Sep 25 '15 at 15:09
  • I tried to replace the question mark with # and it's still not working – Ofer Gozlan Sep 25 '15 at 15:10
  • One more link : http://stackoverflow.com/questions/19932755/illegal-characters-in-path-error-when-downloading-csv-file – Ruchi Sep 25 '15 at 15:10
  • 2
    myStringWebResource is "download.finance.yahoo.com/d/quotes.csv?s=%40%5EDJI,aapl&f=o&e=.csvaapl.csv" which seems not correct. –  Sep 25 '15 at 15:10
  • @Will sure, but that still won't throw the given exception. A 404 WebException at best. – CodeCaster Sep 25 '15 at 15:15
  • 1
    I think your problem is `myStringWebResource = remoteUri + fileName;`, just don't do that. Then replace the last line with `myWebClient.DownloadFile(remoteUri, fileName);` – bkribbs Sep 25 '15 at 15:18

2 Answers2

3

This works:

string remoteUri = @"http://download.finance.yahoo.com/d/quotes.csv?s=%40%5EDJI,aapl&f=o&e=.csv";
string fileName = @"c:\aapl.csv";

// Create a new WebClient instance.
WebClient myWebClient = new WebClient();       

// Download the Web resource and save it into the current filesystem folder.
myWebClient.DownloadFile(remoteUri, fileName);
Steve Wellens
  • 20,506
  • 2
  • 28
  • 69
2

Ok. Lets see your code.

// Dont forget the "http://". A lot of browser add it themselves but the WebClient doesnt.
string remoteUri = "download.finance.yahoo.com/d/quotes.csv?s=%40%5EDJI,aapl&f=o&e=.csv";

// I recommend to take the habitude to write each one in one line.
string fileName = "aapl.csv", myStringWebResource = null;

// Use the "using" keyword to dispose WebClient
WebClient myWebClient = new WebClient();

// Why are you doing this? Your url is working without. No need to concat here.
myStringWebResource = remoteUri + fileName;

// Download the Web resource and save it into the current filesystem folder.
myWebClient.DownloadFile(myStringWebResource, fileName);

Solution to test : (Demo on .NETFiddle)

using System;
using System.Net;

public class Program
{
    public void Main()
    {
        string remoteUri = "http://download.finance.yahoo.com/d/quotes.csv?s=%40%5EDJI,aapl&f=o&e=.csv";

        using (var myWebClient = new WebClient())
        {
            string csv = myWebClient.DownloadString(remoteUri);
            Console.WriteLine(csv);
        }   
    }
}

Solution to your problem :

using System;
using System.Net;

public class Program
{
    public void Main()
    {
        string remoteUri = "http://download.finance.yahoo.com/d/quotes.csv?s=%40%5EDJI,aapl&f=o&e=.csv";
        string fileName = "aapl.csv";

        using (var myWebClient = new WebClient())
        {
            myWebClient.DownloadFile(remoteUri, fileName);
        }   
    }
}
aloisdg
  • 22,270
  • 6
  • 85
  • 105