ojek
ojek

Reputation: 10068

how to encode string so that it can be sent via http?

I have this piece of string in my POST request:

"signature=bI�(��0�P���h;"

Now, i would like to encode it, since my WebResponse class doesn't want to send it over. I tried using HttpUtility.HtmlEncode(), but that method doesn't want to cooperate (I mean string stays intact).

How can i encode those weird characters so that it will go through http?

Edit: Okay nvm, I just found out that there is also a method called .UrlEncode(), which works as intended, sorry for trouble!

Upvotes: 2

Views: 137

Answers (3)

Marc Gravell
Marc Gravell

Reputation: 1062780

If you mean "post as a form value", then that is a fairly standard thing to do. Why not let a library component worry about that:

using (var client = new WebClient())
{
   var vals = new NameValueCollection();
   vals["signature"] = yourString;
   // other form inputs here...
   client.UploadValues(url, "post", vals);
}

Upvotes: 1

Parimal Raj
Parimal Raj

Reputation: 20575

You can use the HttpUtility.UrlEncode.

HttpUtility.UrlEncode - URL encoding ensures that all browsers will correctly transmit text in URL strings. Characters such as a question mark (?), ampersand (&), slash mark (/), and spaces might be truncated or corrupted by some browsers. As a result, these characters must be encoded in tags or in query strings where the strings can be re-sent by a browser in a request string.

Upvotes: 2

bizzehdee
bizzehdee

Reputation: 21003

.NET also has URL Encode: http://msdn.microsoft.com/en-us/library/zttxte6w.aspx or look up HttpUtility.UrlEncode

Upvotes: 4

Related Questions