C# 如何将带有查询字符串的 URL 作为查询字符串发送

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10431107/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 13:49:43  来源:igfitidea点击:

How do I send a URL with Query Strings as a Query String

c#urlquery-string

提问by Dov Miller

I am doing a redirect from one page to another and another redirect from the second page to a third. I have imformation from the first page which is not used on the second page but must be transfered to the third page. Is it possible to send the URL of the third page with its Query Strings as a Query String to the second page. Here's an example:

我正在从一个页面重定向到另一个页面,另一个从第二个页面重定向到第三个页面。我有来自第一页的信息,它没有在第二页上使用,但必须转移到第三页。是否可以将第三页的 URL 及其查询字符串作为查询字符串发送到第二页。下面是一个例子:

Response.Redirect("MyURL1?redi=MyURL2?name=me&ID=123");

My problem is that the URL being sent as a Query String has two Query String variables, so how will the system know that what's after the & is the second variable of the second URL and not a second variable of the first URL? Thank you.

我的问题是作为查询字符串发送的 URL 有两个查询字符串变量,那么系统如何知道 & 后面是第二个 URL 的第二个变量而不是第一个 URL 的第二个变量?谢谢你。

采纳答案by Remko Jansen

You must encode the url that you pass as a parameter in your redirect URL. Like this:

您必须对作为重定向 URL 中的参数传递的 url 进行编码。像这样:

MyURL = "MyURL1?redi=" + Server.UrlEncode("MyURL2?name=me&ID=123");

This will create a correct url without the double '?' and '&' characters:

这将创建一个没有双 '?' 的正确网址。和 '&' 字符:

MyURL1?redi=MyURL2%3fname%3dme%26ID%3d123

See MSDN: HttpServerUtility.UrlEncode Method

参见 MSDN:HttpServerUtility.UrlEncode 方法

To extract your redirect url from this encoded url you must use HttpServerUtility.UrlDecodeto turn it into a correct url again.

要从此编码的 url 中提取重定向 url,您必须HttpServerUtility.UrlDecode再次将其转换为正确的 url。

回答by empi

Your query string should look like this:

您的查询字符串应如下所示:

MyURL1?redi=MyURL2&name=me&ID=123

Check: http://en.wikipedia.org/wiki/Query_string

检查:http: //en.wikipedia.org/wiki/Query_string

You should have one ? sign and all parameters joined with &. If parameter values contain special characters just UrlEncodethem.

你应该有一个?符号和所有参数用 & 连接。如果参数值包含特殊字符,只需对它们进行UrlEncode

回答by JulioHM

I find it helpful to encode query string parameters in Base64 before sending. In some cases this helps, when you need to send all kinds of special characters. It doesn't make for good debug strings, but it will protect ANYTHING you are sending from getting mixed with any other parameters.

我发现在发送之前在 Base64 中编码查询字符串参数很有帮助。在某些情况下,当您需要发送各种特殊字符时,这会有所帮助。它不会产生好的调试字符串,但它会保护您发送的任何内容免于与任何其他参数混合。

Just keep in mind, the other side who is parsing the query string will also need to parse the Base64 to access the original input.

请记住,解析查询字符串的另一方也需要解析 Base64 才能访问原始输入。

回答by Amin Malakoti Khah

using System.IO;
using System.Net;

static void sendParam()
{

    // Initialise new WebClient object to send request
    var client = new WebClient();

    // Add the QueryString parameters as Name Value Collections
    // that need to go with the HTTP request, the data being sent
    client.QueryString.Add("id", "1");
    client.QueryString.Add("author", "Amin Malakoti Khah");
    client.QueryString.Add("tag", "Programming");

    // Prepare the URL to send the request to
    string url = "http://026sms.ir/getparam.aspx";

    // Send the request and read the response
    var stream = client.OpenRead(url);
    var reader = new StreamReader(stream);
    var response = reader.ReadToEnd().Trim();

    // Clean up the stream and HTTP connection
    stream.Close();
    reader.Close();
}