简体   繁体   English

C# 使用 VMware SDK 将文件发送到 VM 目录 InitiateFileTransferToGuest 协议错误 --&gt; 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>

I am trying to send a file to a VM on a ESXi Server from an outside Client.我正在尝试从外部客户端向 ESXi 服务器上的虚拟机发送文件。 I am using the VMware SDK VMWare.Vim.dll.我正在使用 VMware SDK VMWare.Vim.dll。 When i try to use the URL given by the FileTransfer Request it Fails.当我尝试使用 FileTransfer 请求提供的 URL 时,它失败了。 Code:代码:

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 Looks like: " https://199.199.0.1:443/guestFile?id=22&token=23e2deef-7979-b1a8-75e5-413440d8c1377 " Cookie Looks like: "vmware_soap_session="5e24eaa312s1245de2gg213456m23b4bd87c8e1ca" URL 看起来像:“ https://199.199.0.1:443/guestFile?id=22&token=23e2deef-7979-b1a8-75e5-413440d8c1377 ” Cookie 看起来像:“vmware_soap_session="5e24ea21534b1g28c8c8c8c8c8c8c8c75e5c

WebException: The remote Server returned an error:(500) Internal Server Error WebException:远程服务器返回错误:(500) 内部服务器错误

ResponseStream: \\n 22\\n The file Name is not valid ResponseStream: \\n 22\\n 文件名无效

Anyone any experience on file Transfers with VMware SDK?任何人有使用 VMware SDK 进行文件传输的经验吗? Thank you for your help!感谢您的帮助!

btw.顺便说一句。 it was a simple mistake, all slashs have to be switched with a double backslash.这是一个简单的错误,所有斜杠都必须用双反斜杠切换。 The VMWare sdk can handle the slash, but the webclient put can't. VMWare sdk 可以处理斜杠,但 webclient put 不能。

example:例子:

string targetDir = BasePath + "\\" + Folder;

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C#+访问错误:无效的文件名 - C# + Access Error: Not a valid File name 使用C#的VMware vCenter API - InitiateFileTransferToGuest失败 - VMware vCenter API with C# - InitiateFileTransferToGuest fails c#打开word进程出现“目录名无效”错误 - "The directory name is not valid" error in opening the word process by c# 为什么C#HttpWebRequest会在ResponseStream上返回500错误,而不是PHP? - Why would C# HttpWebRequest return 500 error on ResponseStream, but not with PHP? 在c#中使用vsphere-automation-sdk.net从VMWare禁用VM的网络接口卡中的问题 - Issue in Disable Network interface Card of VM from the VMWare using vsphere-automation-sdk.net in c# 如何在 vmware 上使用 c# 获取 vm 的名称、资源轮询名称、vlan id? - How to got vm's name,resource poll name,vlan id using c# on vmware? 通过C#SDK在存储桶名称中包含句点(点)时,在S3上上传文件时出错 - Error in uploading file on S3 when bucket name containing periods(dots) through c# SDK 有效网址C#上的WebClient 404协议错误 - Webclient 404 protocol error on valid url c# File.Copy错误-C#-IOException文件名,目录名或卷标签 - File.Copy error - C# - IOException The filename, directory name, or volume label 错误:在位置“ /协议” C#Facebook SDK中未找到XAML - ERROR: No XAML Found at the location '/Protocol' C# Facebook SDK
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM