将引号从文本框传递到外部 URL

Pass Quotes from a text box to external URL

我有一个简单的 VB.net 应用程序,它有一个文本框,我将其传递给 URL 进行搜索。所以假设我想发送这个: 搜索“*球”

因此,我的搜索将查找 * 后面带球的所有内容。问题是,当我发送它时它会去掉引号。

System.Diagnostics.Process.Start("https://searchgames.local/search?game=" & TextBox1.Text)

我如何在我的文本框中查找报价,然后如果它们在那里,将它们适当地传递到我要发送到的 URL。在我的代码下方只是确保他们确实在文本框中输入了一些内容。一如既往,我们将不胜感激。

If TextBox1.Text = "" Then ' If user does not enter any text
            MsgBox("Enter text to search on." & vbCrLf & Err.Description, MsgBoxStyle.Information, "Need search term to search on.")
            TextBox1.Focus() 'Set the cursor back to the text box

试试这个:

Dim url        as string = "https://searchgames.local/search?game=" & TextBox1.Text
Dim encodedurl as string = HttpContext.Current.Server.UrlEncode(url)
System.Diagnostics.Process.Start(encodedurl)

如果您使用的是网络应用程序,则可以使用 HttpUtility.UrlEncode to properly escape the quotation mark characters. Outside of a web application, MSDN recommends that you use WebUtility.UrlEncode. You could also use Uri.EscapeDataString, but there seem to be some problems

因此,例如,您可以这样做:

Process.Start("https://searchgames.local/search?game=" & HttpUtility.UrlEncode(TextBox1.Text))

或者这个:

Process.Start("https://searchgames.local/search?game=" & WebUtility.UrlEncode(TextBox1.Text))

或者这个:

Process.Start("https://searchgames.local/search?game=" & Uri.EscapeDataString(TextBox1.Text))