簡體   English   中英

無效的URI:指定了無效的端口/無法解析主機名

[英]Invalid URI: Invalid port specified / The hostname could not be parsed

我正在嘗試將文件上傳到遠程服務器的FTP。

當我嘗試使用時: ftp:// user:pass@ftp.host.com/name.txt

我收到此錯誤: 無效的URI:指定了無效的端口。

當我嘗試使用時: ftp:// [user:pass@ftp.host.com] /name.txt

如幾篇文章中所建議,

我收到此錯誤: 無效的URI:無法解析主機名。

這是我的上傳代碼:

using (System.Net.WebClient webClient = new System.Net.WebClient())
{
    webClient.UploadFile(new Uri(ftpAddress), @"C:\inetpub\wwwroot\name.txt");
}

我猜您的密碼或用戶名包含使uri無效的符號。 因此,您應該使用另一種方式來設置憑據:

var uri = new Uri("ftp://ftp.host.com/name.txt"); //no credentails in url
using (var webClient = new System.Net.WebClient())
{
    webClient.Credentials = new System.Net.NetworkCredential("user", "pass");
    webClient.DownloadFile(uri, @"C:\inetpub\wwwroot\name.txt");
}

或者,您仍然可以在URL中使用密碼,但是您將需要通過以下方式對特殊字符進行編碼:

# -> %23     
/ -> %2F     
: -> %3A     
? -> %3F     
\ -> %5C     
% -> %25

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM