0

This may seem like a simple question but I'm writing a console application that needs to access files on a remote server. I've got a URL, a U/N and a P/W. The URL protocol is https of course and I do not have the option of using ftp or sftp. When I access the remote path with a browser, I am asked for a u/n and p/w so it appears to be basic file authentication.

I've searched for various methods and have found solutions like Impersonation but most involve authenticating over a local network or a Windows network.

Is there any way i can use File.Exists and File.Copy methods on a server with this type of authentication?

The path I have is in the following format..

https://domain.net/folder/

Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
Cody Hicks
  • 420
  • 9
  • 24

1 Answers1

6

You can do this using the WebClient class in the .NET framework.

Try the following:

using (var client = new System.Net.WebClient())
{
    client.Credentials = new System.Net.NetworkCredential("username", "password");

    var localPath = @"c:\file.txt";
    var remotePath = "http://example.com/files/somefile.txt";

    client.DownloadFile(remotePath, localPath);
}

This will download the file from http://example.com/files/somefile.txt and save it to c:\file.txt.

Mun
  • 14,098
  • 11
  • 59
  • 83
  • 1
    +1. Beware of sites with bad auth implementation like mentioned in http://stackoverflow.com/questions/1829609/why-is-use-of-new-networkcredentialusername-password-not-working-for-basic?rq=1 – Alexei Levenkov Jun 12 '14 at 19:57
  • @Mun I'll give that a try. My project requirements seemed fairly simple at first but then I realized I would need to somehow authenticate with the server. I've used an API called ChilKat for sftp and ftp but this will be a first for just basic authentication over https. – Cody Hicks Jun 12 '14 at 19:58
  • @Alexei Levenkov I appreciate the heads up. I'll see what happens with Mun's solution real quick. – Cody Hicks Jun 12 '14 at 20:00
  • @Mun This solution is exaclty what I was looking for. I'm not sure if I was using the correct search terms for a solution like this but again, most people where only interested in authenticating over a local network of some sort. I highly appreciate this. – Cody Hicks Jun 12 '14 at 20:10