Uri.EscapeUriString - How do I use it?

2019-08-05 10:48发布

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.

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

    }

1条回答
家丑人穷心不美
2楼-- · 2019-08-05 10:57

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));
查看更多
登录 后发表回答