Arabic QueryString problem (???? in the value) - asp.net

I am sending an arabic value in a querystring, when retrieving it on the server, the value is erroneous and is replaced by quotation marks (????).
for example:
http://server/mypage.aspx?qs=مرحبا
the value of Request.QueryString("qs") is ?????
Note that Response.Write('مرحبا') executes correctly.
Any idea about this querystring problem?
Thanks.

Just URL Encode the arabic string and it should work fine.
Edit: You must URL Encode the string before putting it in the querystring.
For instance, if you were to url encode the space character, it will appear as %20 in your querystring, like this:
http://foo.com/dosomething?param1=hello%20world
Then when you read param1 you URL Decode it, and you get the string "hello world"
You could also URL Encode every single character but for regular characters it's pointless.

I sent an Arabic text in my query string
and when I resieved this string it was Encoded
after Server.UrlDecode
departmentName = Server.UrlDecode(departmentName);
it back again to arabic
so just use Server.UrlDecode(encodedString);
Hope this help you

I had a similar problem and solved it by putting the following line in my web.config file:
<globalization fileEncoding="windows-1256"
requestEncoding="windows-1256" responseEncoding="windows-1256"/>"
And this in the head section of my HTML page:
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">

The Non english characters can't be passed without being encoded ,
so you need to encode the value before you redirect to the target page as follows:
string text="مرحبا";
text=Server.UrlEncode(text);
string url="http://server/mypage.aspx?qs="+text;
Response.Redirect(url);

Related

Getting none-English character from query string

I have this in my querystring - sug_zehut=ז
(ז is a Hebrew letter)
Although I'm well aware that this is bad practice, I have to receive it like so in my query string (not my code..)
When I write it to a hidden I get sug_zehut=%EF%BF%BD as a part of the querystring, and when I try to put it in a string and put that in a hidden, I get � (I found here that those two are the same).
Anyhow, the question is - How do I get the value ז to my variable?
(I'm using .net version 4)
Thanks.
%EF%BF%BD is the code of � sumbol. That's mean your server don't know this character because of you sent symbol in one encoding and try get it as UTF-8.
Try to set up utf-8 encoding in every place (at least for testing of the problem):
In web.config
http://msdn.microsoft.com/en-us/library/ydkak5b9(v=vs.71).aspx
In the page
<meta charset="utf-8"> vs <meta http-equiv="Content-Type">
Change real file encoding if you directly set up your symbol (.cs,
.cshtml, .aspx, .js, ...)
If it's not working that please tell me how do you get this url and navigate on it?
Here's a small function to convert your character to an HTML encoded version (ז in this case). HtmlEncode won't do it for you in most versions of .net, unfortunately.
private static string UnicodeConvertChar(char input)
{
if (input > 159)
{
return "&#" + ((int)input).ToString() + ";";
}
else
{
return input.ToString();
}
}
You can convert it before setting it in your hidden field, and then when you get it back, you can simply read it by using HttpUtility.HtmlDecode.
Here's a dotnetfiddle for you to play around with it:
https://dotnetfiddle.net/jbDY0V

Response.Redirect Ampersand Encoding

Failing to escape an "&" character in HTML markup creates an entity. It is often done inadvertently when linking URLs in a document, and W3C's Markup Validation Service will consider this an error.
I'm wondering, does ASP.NET's Response.Redirect method expect ampersands to be escaped in its url parameter? From reading its MSDN description, I honestly can't tell.
Pass the URL exactly as it should appear in the address bar in the web browser. For example, if you're trying to redirect to http://example.com/?foo=bar&baz=quux, then pass that exact string as-is to Response.Redirect.
try UrlEncode The UrlEncode(String) method can be used to encode the entire URL, including query-string values. If characters such as blanks and punctuation are passed in an HTTP stream without encoding, they might be misinterpreted at the receiving end. URL encoding converts characters that are not allowed in a URL into character-entity equivalents; URL decoding reverses the encoding. For example, when the characters < and > are embedded in a block of text to be transmitted in a URL, they are encoded as %3c and %3e. URLEncode
System.Web.HttpUtility.UrlEncode(string url)

Decode and RequestQueryString

My URL is
www.domainname.com/default.aspx?l=en&t=32600483-1618-4f09-9a86-c12de4dafc7b
I would love to read the QueryString value of t. So i can parse it as GUID.
Unfortunatelly that & thing, seems to mess things up.
How can i parse the value of the t Query string?
My URL is
I hope you realize that this is not a valid URL. This is a HTML encoded string. Do not confuse with an URL. URLs should be properly URL encoded, not HTML encoded. And since you start with something invalid your only chance is to use some ugly string parsing/regex to extract the necessary information. Since this looks like an HTML encoded string you could HTML decode it first:
var myUrl = "http://www.domainname.com/default.aspx?l=en&t=32600483-1618-4f09-9a86-c12de4dafc7b";
myUrl = HttpUtility.HtmlDecode(myUrl);
var values = HttpUtility.ParseQueryString(myUrl);
var t = values["t"];
But I repeat once again: don't do this: tackle the problem at its root. And the root of your problem is the origin of this URL. So if you have control over the generation of this URL then fix it so that you can have a valid URL that you could easily work with with the built-in methods. If you don't have control over the generation of the URL then notify the author of the code that he has a bug in it and ask him to fix it because he has provided you a non-properly encoded URL and you are obliged to use some ugly string parsing mechanisms to extract the information from it.

Special character in HTML output, likely due to an encoding issue

I am seeing a special character in the ASP .NET page I am rendering.
This page reads that content as XML Response from a REST service.
If I load the XML in browser, it displays "-" fine. (It's longer than the usual dash :))
But when print on the ASPX page repeater using EVAL, it displays a special character.
The page has a meta tag.
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
Though, the browser detects the page encoding as UTF-8.
I am looking for a solution so that I can get rid of special character.
The char is probably ASCII code 150 or 151. Some programs (notably MS-Word) use these for dash and long dash. The problem is that charset ISO-8859-1 does not map characters between 128 -159 to any value, so you cannot be sure how the browser will display the character.
The following function (just typed in, not checked) will convert your source string from 8859-1 to UTF-8
function string MakeUTF8String(string SourceStr)
{
byte[] b = Encoding.GetEncoding("iso-8859-1").GetBytes(SourceStr)
return System.Text.Encoding.UTF8.GetString(b);
}

Converting French Characters to ASCII on form submit

Hi there I was wondering if anyone can help with this. I am submitting values to an access database using ASP classic and need to convert French characters to ASCII. I have done it before with an form to email script. here is the code that I used with the first line being the code that writes a value to the database field. any help would be great.
[code]
'--------------------------------------------------------------------------
' Checks form fields and headings for French Characters and replaces them
' with the ASCII equivalent.
'--------------------------------------------------------------------------
rsAddComments.Fields("Customer") = Request.Form("Customer")
body = Replace(body,"À",chr(192))
body = Replace(body,"Á",chr(193))
body = Replace(body,"Â",chr(194))
body = Replace(body,"Î",chr(206))
[/code]
Try to just put the ISO Latin 1 character set on the page:
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
This work for me.
Actually that will not work. It is the way that access reads to text, same as outlook. I had to convert to ascii for outlook. What I did for that was
body = Replace(body,"é",chr(999))
Now I need to replace all of the values from the form fields with the ascii using ASP before it writes to the database. What I don't know is what to put in place of BODY in the above code.

Resources