Error converting urlencode from PHP to C# - c#

I have a class in PHP that encodes this code "?�m�U", using urlencode(), resulting in "%3F%B6%16m%BEU". However, when I try to encode with c# uisng HttpUtility.UrlEncode(), the result is not the same. The method in c# has a second parameter "Encoding.SOMETHING". I've tried each one of the possible paramaters, but it still doesn't work.
Does anyone know how I can fix this?

This one should work:
string utf8Encoded = System.Web.HttpUtility.UrlEncode(YOURURL, Encoding.UTF8);

Related

This.Page.Request reading incomplete encoded params c#

I have this problem,
This Request["MyParam"] working , but this method have a problem with Encoded values from a webrequest like : MyParam=help & development , I get just the 'help' word, someone have an Idea to resolve that issue ?
Encoding.UTF8.GetBytes is used to convert the string into an array of bytes, which is what the WebRequest API expects.
Can also take help of this link:
http://tech.top21.de/techblog/20100421-solving-problems-with-request-parameter-encoding.html

How to convert a string to a url encoded

I want to be able to convert for example ™ to %99 but i dont know what encoding is that
I tried looking at httputility class but i dont get %99 i get other wierd signs can you please help me? thanks
Im using C#
I want to do that so my the login would work with all chars like ™ im using http post method for a vb forum i need first to correct the encoding
EDIT : Not sure but can i just change the Content-Type : application/x-www-form-urlencoded
to something that accepts signs like trademark so it would work?
From the subject it seems that you are trying to encode given string to url string, e.g. changing something like user#email.com to user%40email.com so it can be in a url http://www.example.com?email=user%40gmail.com
Can you provide a little more information?
If you are trying to pass the string through a URL, than I highly recommend the HttpUtility.UrlEncode method to be on the safe side.

C# Deserialize JSON html string

I'm trying to deserialize a JSON object in c#, my problem is that one of the fields can contain html text (I plan on sanitizing it afterwards).
I’m using a JavaScriptSerializer object to deserialize, but I’m getting a “Invalid object passed in“ error (from the JavaScriptSerializer). If I pass plain text for that same field it works fine and the other fields (including a date and an array) in the object also deserialize correctly so it seems like the html is what’s tripping it up.
I’m using JSON.stringify to serialize the Javascript object and I’m passing it to my page via jQuery.
Is there something I’m supposed to do to in order to pass a string that contains html? I’ve tried enclosing it in quotes, but it didn’t help.
As an example of a string that's accepted vs what throws an error: "Test" is fine while
"<div style="text-align: center;">Test</div>" is not.
Strangely <span> tags also seem to be fine.
Can you encode the html with the javascript escape() function before serializing.
You may have to encodeURIComponent in javascript, then HttpServerUtility.UrlDecode in .NET
You can't pass in HTML characters that aren't encoded for security reasons. You can override this in MVC.Net at the application of function level if you feel secure in your source.
just do some replace like this
jsonString.Replace(#"=""\""",#"=\""\""").Replace(#"\""""",#"\""\""").Replace(#"=""""", #"=\""\""")

UTF-8 to Unicode using C#

Help me please. I have problem with encoding response string after GET request:
var m_refWebClient = new WebClient();
var m_refStream = m_refWebClient.OpenRead(this.m_refUri);
var m_refStreamReader = new StreamReader(this.m_refStream, Encoding.UTF8);
var m_refResponse = m_refStreamReader.ReadToEnd();
After calling this code my string m_refResponse is json source with substrings like \u041c\u043e\u0439. What is it? How to encode it for Cyrillic? I am very tired after a lot of attempts.
corrected
Am I missing something here?
What is it?
"\u041c\u043e\u0439" is the String literal representation of Мой. You don't have to do anything more, Strings are Unicode, you've got your Cyrillic already.
(Unless you mean you literally have the sequence \u041c\u043e\u0439, ie. the value "\\u041c\\u043e\\u0439". That wouldn't be the result of an encoding error, that would be something happening at the server, for example it returning a JSON string, since JSON and C# use the same \u escapes. If that's what's happening use a JSON parser.)
I'm not 100% on this, but I would assume you'd have to pass Encoding.Unicode to StreamReader.

Url encoded characters, ViewState and byte[] arrays oh my

I am trying to throw together a screen scraper and keep getting invalid viewstate issues. It appears that during the System.Net.WebClient download of data or the System.Text.UTF8Encoding.Default.GetString call to convert the byte array returned by the WebClient DownloadData call to a string - that strings which match url character codes are being converted.
ie
Url encoded characters strings like %2B are being converted to their normal characters (+ for %2B).
Is this happening in the WebClient class? Is it the way I am converting the byte array to a string?
EDIT:
Based on suggestions I tried changing to the DownloadString call from the WebClient class and the resulting string has converted the character codes to the specific character so it appears WebClient is the culprit.
EDIT 2:
Solved. By making a call to System.Web.HttpUtility.UrlEncode I was able to convert the + back to %2B before sending the viewstate string back up to the server in subsequent requests. I am still at a loss as to where and why the problem was occurring but the server was expecting a viewstate string that contained ...%2B... and was getting ...+... and determining the viewstate to be invalid and throwing the exception. Kudos to Jon & Henk for forcing me to rethink my assumptions.
If you use System.Text.UTF8Encoding.Default then you're not using UTF-8 - you're using the default encoding for the system. It's equivalent to Encoding.Default, but in a more confusing form. Use Encoding.UTF8 to get a UTF-8 encoding... or use WebClient.DownloadString as Henk suggested.
On the other hand, it's not clear what you're trying to download. If you're trying to download geuinely binary data then you shouldn't be trying to convert it to a string at all.
It would help if you would clarify you question - try to provide a lot more context about what's making the requests, what's having problems, etc.
And what happened if you just use WebClient.DownloadString() instead of opening a binary stream?

Categories

Resources