HTTPS服务器上的WebClient身份验证

时间:2014-06-05 14:01:47

标签: c# asp.net http https

我正在使用WebClient类编写一个程序来下载HTTP服务器上的所有文件。我在尝试通过WebClient的Credentials属性进行身份验证时遇到问题。当我在浏览器中导航到此站点(显示的图像是firefox)时,它会以下列方式请求身份验证: enter image description here

以下是我尝试访问此服务器的方法:

WebRequest request = WebRequest.Create("https://url-is-here.org/ftp/some-more-stuff");
request.Credentials = new NetworkCredential("username-here", "password-here");
request.UseDefaultCredentials = false;    //Not sure if these
request.PreAuthenticate = true;           //two lines are needed
WebResponse response = request.GetResponse();

但是,我在调用GetResponse()时说:

时遇到异常
System.Net.WebException: Error getting response stream (Write: The authentication or 
decryption has failed.): SendFailure ---> System.IO.IOException: The authentication or
decryption has failed.

让我感到困惑的部分原因是,尽管该网站称自己为ftp网站(实际上在真实网址中表示如上面的虚拟网址),但网址以https而不是ftp开头。我最初使用的是FtpWebRequest而不是WebRequest,但是当我尝试通过转换FtpWebRequest的结果创建WebRequest.Create()对象时,我才会遇到此异常:

System.InvalidCastException: Cannot cast from source type to destination type.

有什么想法吗?我是这类工作的新手,因此非常感谢修复和新见解......

0 个答案:

没有答案