I am stumped on this scenario. Basically I have an URL that is passed to a aspx page and then I try to get query string from the URL, but what happens is when I try to get the query string from the URL it omits the '+' and replaces it with an whitespace.
My URL = http://localhost:3872/Test.aspx?mt=jan1TNIixxA1+8tl/0vLLg2PPGq0vMOLEhFQNuG4AJU12VMZpnWTrgar82K5UlXatQT9E9EAUet+q7rq7FoTJf+S2JnSbIptgJDY1EZwRPJDTROktfu5zy25oydmSHB6a4oZetV5mI3s+0R7vW8I0S9d765RHdYU2xkRuojHYZU=
Request["mt"] =jan1TNIixxA1 8tl/0vLLg2PPGq0vMOLEhFQNuG4AJU12VMZpnWTrgar82K5UlXatQT9E9EAUet q7rq7FoTJf S2JnSbIptgJDY1EZwRPJDTROktfu5zy25oydmSHB6a4oZetV5mI3s 0R7vW8I0S9d765RHdYU2xkRuojHYZU=
As you can see these two strings are different.
I thought it was the object to string conversion but this does not seem to be the case cause the value of the object has the omitted '+' before conversion.
What can be done to avoid this character replacement (I want to try and avoid string manipulation)
Also what could the cause of this be?
you are getting that because + is the url encoded representation of space " ". If you want to preseve the plus sign in your value you will need to url encode it:
Send that querystring in URL encoded formate and then you will get the expected result.
see: why Request.QueryString replace + with empty char in some cases?
You can use
MyUrl = MyUrl.Replace("+",#"%2B");
The problem is the '+' character is being converted to whitespace if you use httprequest. If you convert it to its hex value, you can pass it with no problem.
You should use HttpUtility.UrlEncode to generate you parameter value. Currently it seems you are using base64 encoding which is not optimal for query parameters.
Use this:
mt=encodeURIComponent(mt);//if mt be --> jan1TNIixxA1+8tl/0vLLg2PPGq0vMOLEhFQNuG4AJU12VMZpnWTrgar82K5UlXatQT9E9EAUet+q7rq7FoTJf+S2JnSbIptgJDY1EZwRPJDTROktfu5zy25oydmSHB6a4oZetV5mI3s+0R7vW8I0S9d765RHdYU2xkRuojHYZU=
Response.Redirect("Test.aspx?"+mt);
this will encode your URL and after this '+' will converted to "%2B" and if you want to read encoded URL it will not converted to space.
from here
If it is really so important to avoid changing the string when you send it, you could chanhe it AFTER you get ir from httprequest. Maybe you could use:
MyUrl = (Request["mt"].Replace(" ","+"));
There is no possibility to pass the space in url, so when you have a space, you can be sure that there was a '+' in there.
You can get the query string using following method
string strQuery = Request.Url.Query;
Related
I'm using AES encryption for the values of a URL. I've sampled it here with only one parameter to demonstrate the problem:
http://localhost:12345/pagename?id=ha3bEv8A%2ffs0goPGeO6NPQ%3d%3d
Request.QueryString["id"] returns "ha3bev8a/fs0gopgeo6npq==" which clearly does not match the value of the encrypted ID. Is something tripping up QueryString?
You are getting a URL-encoded query string, which Request.QueryString["id"] seems to decode for you. You could always just re-encode it:
string decodedId = Request.QueryString["id"];
string reEncodedId = HttpUtility.UrlEncode(decodedId);
The value you are seeing is in fact correct. What might be confusing you, is in the way it is presented. The id value in the URL is encoded in URL Encoding.
Some characters have to be encoded in the URL String in a different way, as they are special characters that sometimes can mess up the way the string is interpreted if they aren't encoded properly.
For example, in the query string you provided:
http://localhost:12345/pagename?id=ha3bEv8A%2ffs0goPGeO6NPQ%3d%3d
The %2f characters are a way to encode the '/' character, while %3d is a way to encode the '=' character.
When you get the value by getting Request.QueryString["id"] , it is decoding it back from an URL Encoded string to raw text.
Check this page for more reference.
https://www.w3schools.com/tags/ref_urlencode.asp
I am trying to encode searches that are sent as querystrings (Response.Redirect("/Find/" + TextBoxSearch.Text);). There is a row in the database with names including / and +, and when that enters the URL things stop working properly. I have tried encoding like this:
String encode = HttpUtility.UrlEncode(TextBoxSearch.Text);
Response.Redirect("/Find/" + encode);
But can' get it to work, what am I missing? Pretend the search value is ex/ex 18+. How could I get this to work as a querystring?
Don't know if this is important but here is how I get the querysting in my Find-page:
IList<string> segments = Request.GetFriendlyUrlSegments();
string val = "";
for (int i = 0; i < segments.Count; i++)
{
val = segments[i];
}
search = val;
I can't even encode spaces properly.
I try:
String encoded = Uri.EscapeDataString(TextBoxSearch.Text);
Response.Redirect("/Find/" + encoded);
But this does not turn spaces in the querystring in to %20. It does transform "/" though.
EDIT: At this point I would be happy to just turn this url localhost/Find/here are spaces in to localhost/Find/here+are+spaces
EDIT: I have been searching and trying solutions for over 5 hours now.
Can anyone just tell me this:
If I redirect like this Response.Redirect("/Find/" + search);
And I make a search like this Social media
I then Get the queryString as the code above using segments.
Now I want to display info about Social media from my database
but at the same time I want the url to say Find/Social+media
PS: Do I need to encode every url-string? or just where I use signs and spaces.
Instead of HttpUtility.UrlEncode use HttpUtility.UrlPathEncode. From the documentation of UrlEncode:
You can encode a URL using with the UrlEncode method or the
UrlPathEncode method. However, the methods return different results.
The UrlEncode method converts each space character to a plus character
(+). The UrlPathEncode method converts each space character into the
string "%20", which represents a space in hexadecimal notation. Use
the UrlPathEncode method when you encode the path portion of a URL in
order to guarantee a consistent decoded URL, regardless of which
platform or browser performs the decoding.
The + character does represent a space in query strings but not in the address part.
I have a C# custom webpart on a sharepoint 2007 page. When clicking on a link in an SSRS report on another page, it sends the user to my custom webpart page with a query string like the following:
?tax4Elem=Docks%20&%20Chargers&ss=EU%20MOVEX&Phase=1&tax3Elem=Play%20IT&tax5Elem=Charger
Take note of the value for "tax4Elem", which is basically "Docks & Chargers". (The ampersand can actually come up in "tax4Elem", "tax3Elem", and "tax5Elem").
I cannot have the ampersand in that value encoded so I will have to work with this.
How do I parse this query string so that it doesn't recognize the "&" in "Docks & Chargers" as the beginning of a key/value pair?
Thanks in Advance!
kate
If you really cannot correct the URL, you can still try to parse it, but you have to make some decisions. For example:
Keys can only contain alphanumeric characters.
There are no empty values, or at least, there is always an equal sign = after the key
Values may contain additional ampersands and question marks.
Values may contain additional equal signs, as long as they don't appear to be part of a new key/value pair (they are not preceded with &\w+)
One possible way to capture these pairs is:
MatchCollection matches = Regex.Matches(s, #"\G[?&](?<Key>\w+)=(?<Value>.*?(?=$|&\w+=))");
var values = matches.Cast<Match>()
.ToDictionary(m => m.Groups["Key"].Value,
m => HttpUtility.UrlDecode(m.Groups["Value"].Value),
StringComparer.OrdinalIgnoreCase);
You can then get the values:
string tax4 = values["tax4Elem"];
Note that if the query string is "invalid" according to our rule, the pattern may not capture all values.
I think you can't parse that string correctly - it has been incorrectly encoded. The ampersand in "Docks & Chargers" should have been encoded as %26 instead of &:
?tax4Elem=Docks%20%26%20Chargers&ss=EU%20MOVEX&Phase=1&tax3Elem=Play%20IT&tax5Elem=Charger
Is it possible to change the code that generated the URL?
Obviously the request is incorrect. However, to work-around it, you can take the original URL, then find the IndexOf of &ss=. Then, find the = sign immediately before that. Decode (with UrlDecode) then reencode (with UrlEncode) the part between the = and &ss= (the value of tax4Elem). Then, reconstruct the query string like this:
correctQueryString = "?tax4Elem=" + reencodedTaxValue + remainderOfQueryString
and decode it normally (e.g. with ParseQueryString) into a NameValueCollection.
Or you can use HttpServerUtility.HtmlDecode method to decode the value to '&' (ampersand) sign
I have a webapp created using C# and asp.net. I placed a parameter value in the querystring with a plus(+) sign. But the plus sign disappear.
How can I include the plus sign(+) in the query string without disappearing?
Please advise.
Thanks.
Edit: added code with UrlEncode
string str = Server.UrlEncode(Requery.QueryString["new"]);
+ sign has a semantic meaning in the query string. It is used to represent a space. Another character that has semantic importance in the query string is & which is used to separate the various var=value pairs in the query string.
Most server side scripts would decode the query parameters before using them, so that a + gets properly converted to a space. Now, if you want a literal + to be present in the query string, you need to specify %2B instead.
+ sign in the query string is URL-decoded to a space. %2B in the query string is URL-decoded to a + sign.
See the difference between
http://www.google.com/search?q=foo+bar
and
http://www.google.com/search?q=foo%2Bbar
In the above examples, Google's server script is URL-decoding the query parameters and then using them to do the search.
URL-encoding is nothing but % sign followed by the hex-code of the special character. For example, we know that the hex code of A is 0x41 (decimal: 65). Try this:
http://www.google.com/search?q=%41
Hope this makes URL-encoding clear.
So, if you want the + sign to be preserved when a JavaScript is fetching a URL with + signs in its query parameters and a server side script would process the query parameters after URL-decoding it, you should URL-encode the query parameters in the URL before using issuing the HTTP get request so that all + signs are converted to %2B's when the request reaches the server side script. Now when the server side script URL-decodes the query string, all %2B's gets converted back to + signs which is what you want.
See Encode URL in JavaScript? to learn how to URL-encode the parameters using JavaScript. Short answer from the discussion there:
var encodedURL = "http://example.com/foo.php?var=" + encodeURIComponent(param);
You should URLEncode your query string values to make sure any special characters are not lost.
Look at HTML URL Encoding Reference
You need to Encode the + sign - It's value should be %2B
I alter my previous statement so no one gets confused!
Create your url using the Server.UrlEncode.
e.g.
string myUrl = "http://myurl?param1=" + Server.UrlEncode("my+param+1");
For the solution, I have applied:
Step 1:Use Server.UrlEncode method for encoding the URL parameter.
Response.Redirect("YourURL?parameter=Server.UrlEncode(parameterValue.ToString().Trim()");
step 2: on another side, you get a string with a plus(+) sign.
var parameter = Request.QueryString["parameterValue"].ToString().Trim();
This is the result: %2beH8 --> +eH8
Other simple way is, Request.Url.ToString().Substring(Request.Url.ToString().IndexOf("=") + 1) assuming that my URL is, http://localhost/MyApp/Activate.aspx?ActivationCode=cHbtqH9P2dDZkx/mYUgFFo7nrNqSFgqdPisAzzu5/nwlEYDOHI+CQw==
before send you parameter, you need check if the parameter contains plus sign, if have you need replace to one flag, for example:
the parameter is: klasjdlkasd+djid3223
can you replace: klasjdlkasdFLAGdjid3223
and when you go convert, you need replace angain
klasjdlkasd+djid3223
Try this, it works for me:
Request.QueryString["new"].Trim();
The solution is to ALWAYS include .Replace(" ", "+") when you request querystring
string s = Request.QueryString["id"].Trim().Replace(" ", "+");
source: http://www.webmasterworld.com/forum47/3238.htm
Add this line in Decrypt Funcation:
strText = strText.Replace(" ", "+");
when I write
Response.Redirect("Default2.aspx?Name=" + TextBox1.Text);
then
string input = Request.QueryString["Name"];
if I write yahoo+music in textbox
the input will be yahoo music why ? and how can I keep the '+' ?
+ is the encoding for space in query strings. To encode + you need to use %2b.
Try UrlEncode which should handle this for you.
A plus in the URL means a space. You should URL encode the value that you put in the URL:
Response.Redirect("Default2.aspx?Name=" + Server.UrlEncode(TextBox1.Text));
I've another way - although a little bit 'tricky' - to reach your goal by passing '+'(or any other special character) in query string
when you pass the query string you write like this:
Response.Redirect("Default.aspx?Name="+TextBox1.Text.Replace("+","_"));
then it will pass Default.aspx?Name=Yahoo_Music
and to request it, just replace again
string input = Request.QueryString["Name"].Replace("_","+");
input will be: Yahoo+Music.
although this way a little bit tricky but sometimes this way very helpful to pass special character in query string.
Thanks