”“x2B”发送数据到服务器后正在转换为空白
我基本上来自java。但我应该修改 ac# 应用程序。该应用程序将文本框的数据发送到服务器。(https 连接)
一切正常。只有一个问题。如果文本框中的文本包含“+”字符,那么在服务器端它们将作为空格到达。意味着,如果文本框包含“amit+gupta”,那么在服务器端它会达到“amit gupta”。
*恐怕还有一些其他组合可能会进一步导致问题。
代码
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);
我的观察
根据我的理解,数据在发送到服务器之前应该首先进行url编码。但...
UrlEncode 丢失
Visual studio 2008 已安装用于开发 Windows 应用程序。在此(我在 3 台不同的机器上尝试过),我无法找到 UrlEncode()。我也探索过 System.Web 但是:-(
我的问题;
- 上面的代码是否有问题
- UrlEncode() 是否仅适用于基于 Web 的项目
- 是否有 UrlEncode() 的替代方案。所以我可以在我的应用程序中使用它,
- 我应该做一些额外的 IDE 设置还是应该添加一些 dll。
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 different machines), I am not able to find UrlEncode(). I had explored System.Web too But :-(
My questions;
- Whether there is something wrong with above code
- Whether UrlEncode() is available for web based project only
- Is there any alternate of UrlEncode(). So i can use it in my application
- Should i do some extra IDE settings or should i add some dll.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您正在寻找 HttpUtility.UrlEncode
System.Web
。这需要添加对 System.Web.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.