将数据从ASPX发布到ASP页面时会删除空格,但将数据发布到ASPX页面时会保留空格

本文关键字:空格 数据 ASPX 保留 ASP 删除 | 更新日期: 2023-09-27 17:58:34

将数据从ASPX发布到ASP页面时会删除空格,但将数据发布到ASPX页面时会保留空格。下面是示例代码

调用程序代码(aspx代码隐藏)

WebRequest request = WebRequest.Create("http://localhost/asppost/asppost.asp");
// Set the Method property of the request to POST.
request.Method = "POST";
// Create POST data and convert it to a byte array.
string postData = "LastName=Ahamed&Addr1=100 Main Street";
byte[] byteArray = Encoding.UTF8.GetBytes(postData);
// Set the ContentType property of the WebRequest.
request.ContentType = "application/x-www-form-urlencoded";
// Set the ContentLength property of the WebRequest.
request.ContentLength = byteArray.Length;
// Get the request stream.
Stream dataStream = request.GetRequestStream();
// Write the data to the request stream.
dataStream.Write(byteArray, 0, byteArray.Length);
// Close the Stream object.
dataStream.Close();
// Get the response.
WebResponse response = request.GetResponse();
// Display the status.
Debug.WriteLine(((HttpWebResponse)response).StatusDescription);
// Get the stream containing content returned by the server.
dataStream = response.GetResponseStream();
// Open the stream using a StreamReader for easy access.
StreamReader reader = new StreamReader(dataStream);
// Read the content.
string responseFromServer = reader.ReadToEnd();
// Display the content.
Debug.WriteLine(responseFromServer);
// Clean up the streams.
reader.Close();
dataStream.Close();
response.Close();

asppost.asp

<%
Dim Lname, AddressLine1
Lname = Request.Form("LastName")
AddressLine1 = Request.Form("AddressLine1")
Response.Write("Last Name: " & Lname)
Response.Write(" Address Line1: " & AddressLine1)
%>

输出

OK
Last Name: Ahamed Address Line1: 100MainStreet

如果我使用下面的HttpUtility.UrlEncode,问题就会得到解决,但我的问题是,当将相同的数据(没有UrlEncode)发布到ASPX页面时,为什么以及如何保留空格?

string postData = "LastName=" + HttpUtility.UrlEncode("Ahamed") + "&AddressLine1=" + HttpUtility.UrlEncode("100 Main Street");

请分享你的想法。

将数据从ASPX发布到ASP页面时会删除空格,但将数据发布到ASPX页面时会保留空格

postData字符串中的数据需要进行URL编码。

postData = "LastName=Ahamed&Addr1=100 Main Street";

需要为:postData = "LastName=Ahamed&Addr1=100+Main+Street";

在代码中,这将类似于:

string postData = "LastName=" + HttpUtility.UrlEncode(lastName);
postData += "&Addr1=" + HttpUtility.UrlEncode(addr1);