Similar to the CSV file which can be downloaded from http://download.finance.yahoo.com/d/quotes.csv?s=RHT
. How can I downloada file which requires authentication?
I can simply use
My.Computer.Network.DownloadFile("http://download.开发者_开发问答finance.yahoo.com/d/quotes.csv?s=RHT,MSFT,NOVL&f=sl1c1d1&e=.csv", "h:\s.csv")
To download the file which is available in public. I tried setting the username and password as per the MSDN documentation but all I get is the HTML content of the login page.
If the site uses Cookie based authentication, you'll need to post the login details to the server, collect the cookies, then pass them up on the request for the file.
That's not as easy as it sounds...
There's an example here
http://blogs.msdn.com/dgorti/archive/2005/08/16/452347.aspx
Phil
It all depends on whether or not the web site will interpret your username/password when you provide it in your HTTP request. Some sites/protocols will, and others won't.
Here's an article that shows how you can download with the WebRequest class.
Downloading Files with the WebRequest and WebResponse Classes
All you need in addition to this article is adding your username/password to the WebRequest. You can do it like this:
// Create a request for the specified remote file name
WebRequest request = WebRequest.Create(remoteFilename);
if (request != null)
{
string username = "your username";
string password = "your password";
request.Credentials = new System.Net.NetworkCredential(username, password);
...
}
Unfortunately, if using this method doesn't work, then Yahoo's finance page doesn't allow providing a username/password automatically, and you'd have to login "the old fashioned way" to be able to download your file.
It sounds like the site you are working with is using forms authentication. Unless you have access to a version that uses realm authentication you will probably need to use HttpWebRequest and fake POST request to the site while you have a CookieContainer so you can retain the token. Then you would be able to include that token in a get request to download the CSV file.
精彩评论