Home > Net >  "The format of the URI could not be determined" when combining absolute and relative URLs
"The format of the URI could not be determined" when combining absolute and relative URLs

Time:11-10

I have a script that accesses an FTP server, and gets a list of files under a directory:

//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[] { "" };
}

Error message:

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)

I can confirm that I can connect to the FTP server, I can confirm that the files exist, and I can confirm that the URI format is correct (at least that I know of).

(EDIT) If you need to see a variable that is used in the function, I can share it, but sensitive information, such as the ip, username and password will not be the same as the real ip username and password.

CodePudding user response:

The "/AdvancedCalender/Calenders/Versions" is a relative URI, so you need to tag is like that:

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