gpt4 book ai didi

c# - Uri.EscapeUriString - 如何使用它?

转载 作者:行者123 更新时间:2023-12-02 04:27:27 24 4
gpt4 key购买 nike

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

}

最佳答案

您可能想使用 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));

关于c# - Uri.EscapeUriString - 如何使用它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25885353/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com