通过WebClient在FTP凭据中使用特殊字符(斜杠) [英] Using special characters (slash) in FTP credentials with WebClient
问题描述
我发现绝对是引起问题的密码.我的密码中有一个正斜杠,并且无法弄清楚该如何接受它.我已经尝试用%5B
替换它.不能更改密码.
I have discovered it's definitely the password that is causing issues. I have a forward slash in my password and cannot figure out how to get this to accept it. I've already tried replacing it with %5B
. Changing the password is not a possibility.
cd v:
$username = "*********"
$password = "*********"
$usrpass = $username + ":" + $password
$webclient = New-Object -TypeName System.Net.WebClient
function ftp-test
{
if (Test-Path v:\*.204)
{
$files = Get-ChildItem v:\ -name -Include *.204 | where { ! $_.PSIsContainer } #gets list of only the .204 files
foreach ($file in $files)
{
$ftp = "ftp://$usrpass@ftp.example.com/IN/$file"
Write-Host $ftp
$uri = New-Object -TypeName System.Uri -ArgumentList $ftp
$webclient.UploadFile($uri, $file)
}
}
}
ftp-test
运行上面的代码时,我得到
When I run the above code I get
Exception calling "UploadFile" with "2" argument(s): "An exception occurred during a WebClient request."
At line:13 char:34
+ $webclient.UploadFile <<<< ($uri, $file)
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : DotNetMethodException
我不确定是什么问题.搜索带来了代理问题,但是我没有代理需要通过.
I'm not sure what the issue is. Searching has brought issue with proxies, but I have no proxy that I need to get through.
我可以使用ftp.exe
手动上传文件,但是我宁愿在PowerShell中执行所有这些操作,而不是生成脚本来使用ftp.exe
.
I can manually upload the file with ftp.exe
, but I'd rather do all this in PowerShell if possible instead of generating a script to use ftp.exe
with.
推荐答案
您必须 URL-编码特殊字符. 请注意,已编码的斜杠(/
)是%2F
,而不是%5B
(即[
).
You have to URL-encode the special characters. Note that encoded slash (/
) is %2F
, not %5B
(that's [
).
$usrpass = $username + ":" + [System.Uri]::EscapeDataString($password)
或使用 WebClient.Credentials
属性,并且您无需转义任何内容:
Or use the WebClient.Credentials
property, and you do not need to escape anything:
$webclient.Credentials = New-Object System.Net.NetworkCredential($username, $password)
...
$ftp = "ftp://ftp.example.com/IN/$file"
与(Ftp)WebRequest
类似:在我的PowerShell FTP脚本中转义@字符
Similarly for (Ftp)WebRequest
: Escape the @ character in my PowerShell FTP script
这篇关于通过WebClient在FTP凭据中使用特殊字符(斜杠)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!