[英]How do I send a URL with Query Strings as a Query String
我正在從一個頁面重定向到另一個頁面,另一個重定向從第二頁到第三頁。 我從第一頁獲得的信息在第二頁上沒有使用但必須轉移到第三頁。 是否可以將第三頁的URL及其查詢字符串作為查詢字符串發送到第二頁。 這是一個例子:
Response.Redirect("MyURL1?redi=MyURL2?name=me&ID=123");
我的問題是,作為查詢字符串發送的URL有兩個查詢字符串變量,那么系統如何知道&之后的內容是第二個URL的第二個變量而不是第一個URL的第二個變量? 謝謝。
您必須將傳遞的URL編碼為重定向URL中的參數。 像這樣:
MyURL = "MyURL1?redi=" + Server.UrlEncode("MyURL2?name=me&ID=123");
這將創建一個沒有雙''的正確網址。 和'&'字符:
MyURL1?redi=MyURL2%3fname%3dme%26ID%3d123
請參閱MSDN: HttpServerUtility.UrlEncode方法
要從此編碼的網址中提取重定向網址,您必須使用HttpServerUtility.UrlDecode
將其再次轉換為正確的網址。
您的查詢字符串應如下所示:
MyURL1?redi=MyURL2&name=me&ID=123
檢查: http : //en.wikipedia.org/wiki/Query_string
你應該有一個嗎? 標志和所有與&連接的參數。 如果參數值包含特殊字符,則只需對其進行UrlEncode 。
我發現在發送之前在Base64中編碼查詢字符串參數很有幫助。 在某些情況下,當您需要發送各種特殊字符時,這會有所幫助。 它不能提供良好的調試字符串,但它會保護您發送的任何其他參數混合的發送。
請記住,解析查詢字符串的另一方還需要解析Base64以訪問原始輸入。
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();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.