C# 使用 VMware SDK InitiateFileTransferToGuest 协议将文件发送到 VM 目录错误 --> ResponseStream 错误 22 <The File Name is not valid>

C# Sending File to VM Directory using VMware SDK InitiateFileTransferToGuest Protocol Error --> ResponseStream Error 22 <The File Name is not valid>

我正在尝试从外部客户端向 ESXi 服务器上的虚拟机发送文件。 我正在使用 VMware SDK VMWare.Vim.dll。 当我尝试使用 FileTransfer 请求给出的 URL 时,它失败了。 代码:

public static string ServerIP
{
    get { return ConfigurationManager.AppSettings["ServerIP"].ToString(); }
}

private string ServiceUrl = "https://" + ServerIP + "/sdk";
private string UserName = "ESXIUser";
ServiceContent sc = new ServiceContent();
VimClient client = new VimClientImpl();

public VmOps()
{
}

public void Connect(string password)
{
    // connect to vSphere web service
    sc = client.Connect(ServiceUrl);
    // Login using username/password credentials
    client.Login(UserName, password);
}

public void copyDatastoreFile(/*string VMName, string Folder, string sourceFilePath*/)
{
    string hostpass = "ESXIUserPAssword";
    string VMName = "VMname";
    string Folder = "Test";
    string FileName = "test.zip";
    //File destination info
    string BasePath = "D:";
    String targetDir = BasePath + "/" + Folder;
    string srcPath = BasePath + "/" + Folder + "/" + FileName;

    //Connect to the ESXi
    Connect(hostpass);
    NameValueCollection nvcFilter = new NameValueCollection();
    nvcFilter.Add("Name", VMName);
    var vm = (VirtualMachine)client.FindEntityView(typeof(VirtualMachine), null, nvcFilter, null);

    GuestOperationsManager gom = new GuestOperationsManager(client, sc.GuestOperationsManager);
    gom.UpdateViewData();
    if (gom.FileManager == null)
    {
        return;
    }
    GuestFileManager gfm = new GuestFileManager(client, gom.FileManager);

    var Auth = new NamePasswordAuthentication { Password = "VMPassword", Username = "VMUSER", InteractiveSession = false };

    bool exists = false;

    GuestListFileInfo fInfo = gfm.ListFilesInGuest(vm.MoRef, Auth, BasePath, null,null, Folder);
    exists = fInfo.Files != null;
    if (!exists)
    {
        // Create directory where the files will be copied to
        gfm.MakeDirectoryInGuest(vm.MoRef, Auth, targetDir, true);
    }

    // Copy Virtual Machine file To
    string URL = gfm.InitiateFileTransferToGuest(vm.MoRef,
        Auth,
        targetDir + "/" + FileName,
        new GuestFileAttributes(),
        new FileInfo(srcPath).Length,
        true);
    UploadFile(srcPath, URL.Replace("*", ServerIP));            
}        

private void UploadFile(string from, string to)
{
    using (WebClient wc = new WebClient())
    {
        string cookie = ((VimApi_60.VimService)client.VimService).CookieContainer.GetCookies(new Uri(((VimApi_60.VimService)client.VimService).Url))[0].ToString();
        wc.Credentials = new NetworkCredential("ESXiUSER", "ESXIUserPAssword");
        wc.Headers.Add(HttpRequestHeader.Cookie, cookie);

        try
        {
            wc.UploadFile(to, "PUT", from);
        }
        catch (WebException wex)
        {
            if (wex.Response != null)
            {
                string pageContent = new StreamReader(wex.Response.GetResponseStream()).ReadToEnd().ToString();                        
            }
        }
        wc.Dispose();
    }
   }

URL 看起来像: “https://199.199.0.1:443/guestFile?id=22&token=23e2deef-7979-b1a8-75e5-413440d8c1377” 饼干看起来像: "vmware_soap_session="5e24eaa312s1245de2gg213456m23b4bd87c8e1ca

网络异常: 远程服务器返回错误:(500) 内部服务器错误

响应流: \n 22\n 文件名无效

有人使用 VMware SDK 进行文件传输吗?感谢您的帮助!

顺便说一句。这是一个简单的错误,所有斜杠都必须用双反斜杠切换。 VMWare sdk 可以处理斜杠,但是webclient put 不能。

示例:

string targetDir = BasePath + "\" + Folder;

string srcPath = BasePath + *"\" + Folder + "\" + FileName;