简体   繁体   English

Uri.EscapeUriString - 我该如何使用它?

[英]Uri.EscapeUriString - How do I use it?

I am new to C# so i appreciate your help. 我是C#的新手,所以感谢你的帮助。 I have the following code which calls an API. 我有以下代码调用API。 I need to have the URL values encoded. 我需要编码URL值。 Now i have no idea how to do this. 现在我不知道该怎么做。 I would appreciate your assistance. 非常感谢你的帮助。

Thank you. 谢谢。

    private void DeviceDetect_Load(object sender, EventArgs e)
    {

        var printerQuery = new ManagementObjectSearcher("Select * from Win32_Printer");
        foreach (var printer in printerQuery.Get())
        {
            var name = printer.GetPropertyValue("Name");
            var status = printer.GetPropertyValue("Status");
            var isDefault = printer.GetPropertyValue("Default");
            var isNetworkPrinter = printer.GetPropertyValue("Network");
            var description = printer.GetPropertyValue("Description");
            var PortName = printer.GetPropertyValue("PortName");
            var Location = printer.GetPropertyValue("Location");
            var Comment = printer.GetPropertyValue("Comment");


                string macAddress = string.Empty;
                System.Diagnostics.Process pProcess = new System.Diagnostics.Process();
                pProcess.StartInfo.FileName = "arp";
                pProcess.StartInfo.Arguments = "-a " + PortName;
                pProcess.StartInfo.UseShellExecute = false;
                pProcess.StartInfo.RedirectStandardOutput = true;
                pProcess.StartInfo.CreateNoWindow = true;
                pProcess.Start();
                string strOutput = pProcess.StandardOutput.ReadToEnd();
                string[] substrings = strOutput.Split('-');
                if (substrings.Length >= 8)
                {
                    macAddress = substrings[3].Substring(Math.Max(0, substrings[3].Length - 2))
                        + "-" + substrings[4] + "-" + substrings[5] + "-" + substrings[6]
                        + "-" + substrings[7] + "-"
                        + substrings[8].Substring(0, 2);              

                }

                string currentuser = null;
                string appToken = null;
                string password = null;
                XDocument doc = XDocument.Load("config.xml");
                XElement element = doc.Root.Elements("UserID").FirstOrDefault();
                XElement element0 = doc.Root.Elements("Password").FirstOrDefault();
                XElement element1 = doc.Root.Elements("AppToken").FirstOrDefault();

                if (element1 != null)
                {
                    currentuser = element.Value;
                    appToken = element1.Value;
                    password = element0.Value;

                    try
                    {
                        string url = "https://mydomain.com/api/add?t=" + appToken + "&mac=" + macAddress + "&PortName=" + PortName + "&Name=" + name + "&Location=" + Location + "&Description=" + description + "&Comment=" + Comment + "";

                        HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
                        request.Method = "POST";
                        Encoding enc = Encoding.GetEncoding(1252);
                        HttpWebResponse httpWebResponse = (HttpWebResponse)request.GetResponse();
                        StreamReader loResponseStream = new StreamReader(httpWebResponse.GetResponseStream(), enc);
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                        label7.Text = "Status: Error";
                    }
                }        
           }                            

    }

You probably want to use HttpUtility.UrlEncode() . 您可能想要使用HttpUtility.UrlEncode() Not Uri.EscapeUriString() .. 不是Uri.EscapeUriString() ..

string url = string.Format("https://mydomain.com/api/add?t={0}&mac={1}&portName={2}&name={3}&location={4}&description={5}&comment={6}",
            HttpUtility.UrlEncode(appToken),
            HttpUtility.UrlEncode(macAddress),
            HttpUtility.UrlEncode(PortName),
            HttpUtility.UrlEncode(name),
            HttpUtility.UrlEncode(Location),
            HttpUtility.UrlEncode(description),
            HttpUtility.UrlEncode(Comment));

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM