"The format of the URI could not be determined" 在 C# 中组合绝对和相对 URL 时
"The format of the URI could not be determined" when combining absolute and relative URLs in C#
我有一个脚本可以访问 FTP 服务器,并获取目录下的文件列表:
//line 65 of Status script that is mentioned in error message
string[] list = ftp.DirectoryListSimple("AdvancedCalender/Calenders/Versions");
//ftp.DirectoryListSimple()
//FTP script that is mentioned in error message
public string[] DirectoryListSimple(string directory)
{
try
{
Uri serverUri = new Uri(host);
Uri relativeUri = new Uri("/" + directory);
Uri fullUri = new Uri(serverUri, relativeUri);
ftpRequest = (FtpWebRequest)FtpWebRequest.Create(fullUri);
ftpRequest.Credentials = new NetworkCredential(user, pass);
ftpRequest.UseBinary = true;
ftpRequest.UsePassive = true;
ftpRequest.KeepAlive = true;
ftpRequest.Method = WebRequestMethods.Ftp.ListDirectory;
ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();
ftpStream = ftpResponse.GetResponseStream();
StreamReader ftpReader = new StreamReader(ftpStream);
string directoryRaw = null;
try { while (ftpReader.Peek() != -1) { directoryRaw += ftpReader.ReadLine() + "|"; } }
catch (Exception ex) { Debug.LogError(ex.ToString()); }
ftpReader.Close();
ftpStream.Close();
ftpResponse.Close();
ftpRequest = null;
try { string[] directoryList = directoryRaw.Split("|".ToCharArray()); return directoryList; }
catch (Exception ex) { Debug.LogError(ex.ToString()); }
}
catch (Exception ex) { Debug.LogError(ex.ToString()); }
//^ error caught here
return new string[] { "" };
}
错误信息:
System.UriFormatException: Invalid URI: The format of the URI could not be determined.
at System.Uri.CreateThis (System.String uri, System.Boolean dontEscape, System.UriKind uriKind) [0x0007b] in :0
at System.Uri..ctor (System.String uriString) [0x00014] in :0
at BtStudios.HelperClasses.FtpHelpers.FTP.DirectoryListSimple (System.String directory) [0x0000b] in C:\Users\btowr\OneDrive\Desktop\Advanced Calender\Assets\FTP.cs:299
UnityEngine.Debug:LogError (object)
BtStudios.HelperClasses.FtpHelpers.FTP:DirectoryListSimple (string) (at Assets/FTP.cs:330)
Status:Start () (at Assets/Status.cs:65)
我可以确认我可以连接到 FTP 服务器,我可以确认文件存在,我可以确认 URI 格式是正确的(至少我知道)。
(EDIT) 如果你需要查看函数中使用的变量,我可以分享,但敏感信息,如 ip、用户名和密码将与真实 ip 用户名和密码不同密码。
"/AdvancedCalender/Calenders/Versions"
是一个相对 URI,所以你需要这样标记:
Uri relativeUri = new Uri("/" + directory, UriKind.Relative);
我有一个脚本可以访问 FTP 服务器,并获取目录下的文件列表:
//line 65 of Status script that is mentioned in error message
string[] list = ftp.DirectoryListSimple("AdvancedCalender/Calenders/Versions");
//ftp.DirectoryListSimple()
//FTP script that is mentioned in error message
public string[] DirectoryListSimple(string directory)
{
try
{
Uri serverUri = new Uri(host);
Uri relativeUri = new Uri("/" + directory);
Uri fullUri = new Uri(serverUri, relativeUri);
ftpRequest = (FtpWebRequest)FtpWebRequest.Create(fullUri);
ftpRequest.Credentials = new NetworkCredential(user, pass);
ftpRequest.UseBinary = true;
ftpRequest.UsePassive = true;
ftpRequest.KeepAlive = true;
ftpRequest.Method = WebRequestMethods.Ftp.ListDirectory;
ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();
ftpStream = ftpResponse.GetResponseStream();
StreamReader ftpReader = new StreamReader(ftpStream);
string directoryRaw = null;
try { while (ftpReader.Peek() != -1) { directoryRaw += ftpReader.ReadLine() + "|"; } }
catch (Exception ex) { Debug.LogError(ex.ToString()); }
ftpReader.Close();
ftpStream.Close();
ftpResponse.Close();
ftpRequest = null;
try { string[] directoryList = directoryRaw.Split("|".ToCharArray()); return directoryList; }
catch (Exception ex) { Debug.LogError(ex.ToString()); }
}
catch (Exception ex) { Debug.LogError(ex.ToString()); }
//^ error caught here
return new string[] { "" };
}
错误信息:
System.UriFormatException: Invalid URI: The format of the URI could not be determined. at System.Uri.CreateThis (System.String uri, System.Boolean dontEscape, System.UriKind uriKind) [0x0007b] in :0 at System.Uri..ctor (System.String uriString) [0x00014] in :0 at BtStudios.HelperClasses.FtpHelpers.FTP.DirectoryListSimple (System.String directory) [0x0000b] in C:\Users\btowr\OneDrive\Desktop\Advanced Calender\Assets\FTP.cs:299 UnityEngine.Debug:LogError (object) BtStudios.HelperClasses.FtpHelpers.FTP:DirectoryListSimple (string) (at Assets/FTP.cs:330) Status:Start () (at Assets/Status.cs:65)
我可以确认我可以连接到 FTP 服务器,我可以确认文件存在,我可以确认 URI 格式是正确的(至少我知道)。
(EDIT) 如果你需要查看函数中使用的变量,我可以分享,但敏感信息,如 ip、用户名和密码将与真实 ip 用户名和密码不同密码。
"/AdvancedCalender/Calenders/Versions"
是一个相对 URI,所以你需要这样标记:
Uri relativeUri = new Uri("/" + directory, UriKind.Relative);