How to convert a string to a url encoded - c#

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.

Related

How can I check if a string is url friendly

I'm making an ecommerce application and I want the user to be able to put content at a URL they have specified. IF a user were to put in something like "/thank-you!", how can I clean the string to either be a valid URL or check this is valid URL format? I would want the url to basically always be hyphened between words so like "/thank-you" from "/thankyou". What's the best approach for achieving such a thing. I'm within c# using .NET MVC 4.
Alas, I cannot comment 'possible duplicate' yet (How to check whether a string is a valid HTTP URL?).
As this must be an answer however, one way to validate a string URL would be using the URI.TryCreate functioanlity. See here also https://msdn.microsoft.com/en-us/library/system.uri.trycreate(v=vs.110).aspx
URI is also the preferred data type for URLs, rather than strings.

Proper way to handle the ampersand character in JSON string send to REST web service

OK,
I am using the System.Runtime.Serialization and the DataContractJsonSerialization.
The problem is that in the request I send a value of a property with the & character. Say, AT&T, and I get a response with error: Invalid JSON Data.
I thought that the escaping would be done inside the library but now I see that the serialization is left untouched the ampersand & character.
Yes, for a JSON format this is valid.
But it will be a problem to my POST request since I need to send this to a server that if contains an ampersand will response with error, hence here I am.
HttpUtility.HtmlEncode is in the System.Web library and so the way to go is using Uri.EscapeUriString. I did this to try, but anyway, and without it all requests are working fine, except an ampersand is in a value.
EDIT: HttpUtility class is ported to the Windows Phone SDK but the prefer way to encode a string should be still Uri.EscapeUriString.
First thought was to get hands dirty and start replacing the special character which would cause a problem in the server, but, I wonder, is there another solution I should do, that it would be efficient and 'proper'?
I should tell that I use
// Convert the string into a byte array.
byte[] postBytes = Encoding.UTF8.GetBytes(data);
To convert the JSON to a byte[] and write to the Stream.
And,
request.ContentType = "application/x-www-form-urlencoded";
As the WebRequest.ContentType.
So, am I messed up for a reason or something I miss?
Thank you.
The problem was that I was encoding the whole request string including the key.
I had a request data={JSON} and I was formatting it, but the {JSON} part should only be encoded.
string requestData = "data=" + Uri.EncodeDataString(json) // worked perfect!
Stupid hole to step into.
Have you tried replacing the ampersand with & for the POST?

Error converting urlencode from PHP to 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);

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 turn string into readable by php server way? (C#)

I know that my server on real form submit turns %CE%EB%E5%E3+%DF%EA%F3%F8%EA%E8%ED into Олег Якушкин . How to peform string transfer from Олег Якушкин into %CE%EB%E5%E3+%DF%EA%F3%F8%EA%E8%ED using C#?
Like this:
Uri.EscapeDataString(str)
Note that this will encode a space character as %20, not +. However, PHP should handle that fine.
HttpUtility.UrlEncode

Categories

Resources