【问题标题】:Uri.EscapeUriString - How do I use it?Uri.EscapeUriString - 我该如何使用它?
【发布时间】:2014-09-17 08:05:11
【问题描述】:

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

谢谢。

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

    }

【问题讨论】:

    标签: c# api url uri html-escape-characters


    【解决方案1】:

    您可能想使用HttpUtility.UrlEncode()。不是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));
    

    【讨论】:

    • 感谢您抽出宝贵时间回答。现在看来我有一个变量类型问题。所有字符串参数都工作正常,但 var 参数出现错误 - 无法将对象转换为字符串。感谢您提供更多帮助。
    • 您需要将对象转换为字符串或至少其字符串表示形式,例如object.ToString()HttpUtility.UrlEncode(Location.ToString());.
    猜你喜欢
    • 1970-01-01
    • 2011-07-27
    • 1970-01-01
    • 1970-01-01
    • 2010-10-18
    • 2011-07-16
    • 2013-12-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多