C # Sending a request with special characters will prevent the line

I am sending a request from one project to another:

WebRequest request = WebRequest.Create(UrlTemplate); request.Timeout = 500000; request.Method = WebRequestMethods.Http.Post; request.ContentType = ContentType; byte[] postData = System.Text.Encoding.ASCII.GetBytes(data); request.ContentLength = postData.Length; Stream newStream = request.GetRequestStream(); // Send the data. newStream.Write(postData, 0, postData.Length); newStream.Flush(); newStream.Close(); 

Well, in the function installed in UrlTemplate , I get the correct string (which I send to the data), but the problem starts when the string contains special characters.

If line: 12&34

What I get in the function: 12 .

If line: 12+34

What I get in the function: 12 34 .

I will be happy to know if this happened to some of you and how you decided it. Thanks in advance.

+4
source share
3 answers

The problem is caused by the url-form Encoded string. (If it were just GET, I would say that it is encoded at the URL, but you are using POST.) The sign and sign have special meaning in the URL, and the + sign is used as a replacement for spaces.

See http://www.w3.org/TR/html401/interact/forms.html#h-17.13.3.3 for specifications. You will need to code them manually using UrlEncode () if you want the expected results.

+1
source

Use System.Web.HttpUtility.UrlEncode or System.Net.WebUtility.UrlEncode when generating UrlTemplate

+7
source

Use the URI.EscapeDataString () method in your password. This will help you read your special character correctly.

+1
source

All Articles