如何从使用基本身份验证的远程服务器复制文件

Cod*_*cks 0 c# authentication httpwebrequest

这似乎是一个简单的问题,但我正在编写一个需要访问远程服务器上的文件的控制台应用程序.我有一个URL,一个U/N和一个P/W. URL协议当然是https,我没有使用ftp或sftp的选项.当我使用浏览器访问远程路径时,我被要求提供au/n和p/w,因此它似乎是基本的文件身份验证.

我搜索了各种方法,并找到了像Impersonation这样的解决方案,但大多数都涉及通过本地网络或Windows网络进行身份验证.

有没有办法在这种类型的身份验证的服务器上使用File.ExistsFile.Copy方法?

我的路径采用以下格式..

https://domain.net/folder/

Mun*_*Mun 6

您可以使用WebClient.NET框架中的类来完成此操作.

请尝试以下方法:

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);
}
Run Code Online (Sandbox Code Playgroud)

这将从中下载文件http://example.com/files/somefile.txt并将其保存到c:\file.txt.