”“x2B”发送数据到服务器后正在转换为空白

发布于 2024-11-14 03:08:37 字数 1221 浏览 1 评论 0原文

我基本上来自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 但是:-(

我的问题;

  1. 上面的代码是否有问题
  2. UrlEncode() 是否仅适用于基于 Web 的项目
  3. 是否有 UrlEncode() 的替代方案。所以我可以在我的应用程序中使用它,
  4. 我应该做一些额外的 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;

  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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

披肩女神 2024-11-21 03:08:37

您正在寻找 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文