开发者

"+" is being converted to blank space after sending data to server

开发者 https://www.devze.com 2023-03-11 22:38 出处:网络
I am from java basically. But i supposed to modify a c# application. This application sends data of a text box to a server.(https connection)

I am from java basically. But i supposed to modify a c# application. This application sends data of a text box to a server.(https connection)

everything is working fine.There is only one problem. If text in textbox is having "+" characters then on server side they are reaching as space. Means, if text box contains "amit+gupta" then on server side it is reaching as "amit gupta".

*I afraid there might be some other combinations which may cause problem further.

Code

System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();
StringBuilder postData = new StringBuilder();
postData.Append("data1="+textBox4.Text);
postData.Append("&data2="+textBox5.Text);
byte[] data = encoding.GetBytes(postData.ToString());

HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(serverUrl);
myRequest.Method = "POST";
myRequest.ContentType = "application/x-www-form-urlencoded";
myRequest.ContentLength = data.Length;

Stream newStream = myRequest.GetRequestStream();
newStream.Write(data, 0, data.Length);

My observation

As per my understanding, data should be url encoded first before sending to server. But...

UrlEncode is missing

Visual studio 2008 has been setup for developing windows applicaion. In this(I had tried on 3 di开发者_如何学Cfferent machines), I am not able to find UrlEncode(). I had explored System.Web too But :-(

My questions;

  1. Whether there is something wrong with above code
  2. Whether UrlEncode() is available for web based project only
  3. Is there any alternate of UrlEncode(). So i can use it in my application
  4. Should i do some extra IDE settings or should i add some dll.


You're looking for HttpUtility.UrlEncode in System.Web.

This requires adding a reference to System.Web.dll. If you can't add a reference to that, make sure you have the complete profile (not the client profile). If you require the client profile, you will have to implement your own encoding function.

0

精彩评论

暂无评论...
验证码 换一张
取 消