在C#中合并绝对和相对URL时,“URI的格式无法确定”。

4
我有一个脚本可以访问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:无效的URI:无法确定URI的格式。 在System.Uri.CreateThis(System.String uri,System.Boolean dontEscape,System.UriKind uriKind)[0x0007b] in :0 在System.Uri..ctor(System.String uriString)[0x00014] in :0 在BtStudios.HelperClasses.FtpHelpers.FTP.DirectoryListSimple(System.String directory)[0x0000b]中 C:\Users\btowr\OneDrive\Desktop\Advanced Calender\Assets\FTP.cs:299 UnityEngine.Debug:LogError (object) BtStudios.HelperClasses.FtpHelpers.FTP:DirectoryListSimple (string)(位于Assets/FTP.cs:330) 状态:Start()(位于Assets/Status.cs:65)

我可以确认我能够连接到FTP服务器,我可以确认文件存在,并且我可以确认URI格式正确(至少我所知道的是正确的)。

(编辑)如果您需要查看函数中使用的变量,我可以分享它,但是敏感信息,例如IP、用户名和密码将与真实的IP、用户名和密码不同。

1个回答

0

"/AdvancedCalender/Calenders/Versions" 是一个 相对 URI,所以你需要这样标记:

Uri relativeUri = new Uri("/" + directory, UriKind.Relative);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接