2015-10-09 120 views
0

我在写一个PowerShell脚本,它应该从链接下载一个.csv文件。错误403 - 下载失败

我想它是这样的:

$us = "user" 
$pw = "password" 
$webclient = New-Object System.Net.WebClient 
$webclient.Credentials = New-Object System.Net.Networkcredential($us, $pw) 
$webclient.DownloadFile($url,$pfad) 

,但我得到了以下403错误:

enter image description here

谁能告诉我什么,我做错了什么? PS:我可以从另一个页面下载.png。如果我在浏览器中输入链接,我也可以下载这个.csv文件。

编辑12.10.15:
我发现了一个错误。我在URL中有“”。现在我用“%20”替换了所有“”。
但我仍然有这样的错误:
enter image description here

回答

2

你的代码看起来OK我,但用户似乎并不具有对文件的访问。见Wikipedia article上的错误403:

Difference from status "401 Unauthorized"

Status codes 401 (Unauthorized) and 403 (Forbidden) have distinct meanings.
[...]
A 403 response generally indicates one of two conditions:

  • Authentication was provided, but the authenticated user is not permitted to perform the requested operation.
  • The operation is forbidden to all users. For example, requests for a directory listing return code 403 when directory listing has been disabled.

你可能需要检查服务器日志中关于为何请求被拒绝的详细信息。

+0

很糟糕,我没有在服务器上的任何错误日志:/我想我应该有正确的权限。因为如果我采用生成的url并使用IE打开它,我可以下载该文件。 – npit

+0

没有关于为什么服务器认为你不被允许访问该文件的信息,没有什么我可以告诉你的。 –

0

它看起来像远程服务器不喜欢你。 您可能需要在代码中添加一些凭据以确认您的身份。 您是否可以连接到服务器而无需使用任何用户名或密码的详细信息?

+0

是的,如果我在我的IE中键入链接,我可以下载没有凭据的文件。 – npit