本文介绍了Uri.EscapeUriString - 如何使用它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 C# 新手,所以感谢您的帮助.我有以下调用 API 的代码.我需要对 URL 值进行编码.现在我不知道该怎么做.感谢您的帮助.

I am new to C# so i appreciate your help. I have the following code which calls an API. I need to have the URL values encoded. Now i have no idea how to do this. I would appreciate your assistance.

谢谢.

    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";
                    }
                }
           }

    }

推荐答案

您可能想要使用 HttpUtility.UrlEncode().不是 Uri.EscapeUriString()..

You probably want to use HttpUtility.UrlEncode(). Not 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));

这篇关于Uri.EscapeUriString - 如何使用它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-11 12:52