我们通过IIS 7.0发布了一个文件夹,如下所示,并在其中放置了一些文件。
如果我们通过浏览器访问以下文件,我们就可以看到它。
现在我们需要编写一个程序来下载和上传文件到这个文件夹中,我们编写的代码如下:
https://www.example.net/mydocs
如果我们通过浏览器访问以下文件,我们就可以看到它。
https://www.example.net/mydocs/client.xml
https://www.example.net/mydocs/log.jpg
现在我们需要编写一个程序来下载和上传文件到这个文件夹中,我们编写的代码如下:
WebClient webClient = new WebClient();
string webAddress = null;
try
{
webAddress = @"https://www.example.net/mydocs";
webClient.UseDefaultCredentials = true;
webClient.Credentials = CredentialCache.DefaultCredentials;
WebRequest serverRequest = WebRequest.Create(webAddress);
WebResponse serverResponse;
serverResponse = serverRequest.GetResponse();
serverResponse.Close();
webClient.UploadFile(webAddress + @"1.xml", "PUT", @"C:\d\1.xml");
webClient.Dispose();
webClient = null;
}
catch (Exception error)
{
MessageBox.Show(error.Message);
}
但是在serverResponse = serverRequest.GetResponse();
处出现错误。
错误信息为:远程服务器返回错误:(403)禁止访问。
如果我们尝试访问
https://www.example.net/mydocs
通过浏览器,我们遇到了错误
403 - 禁止访问:拒绝访问。您没有使用提供的凭据查看此目录或页面的权限。当访问通过iis发布的文件夹时发生此错误。