我对这种方法有疑问,出于某种原因,我向我的 asp 页面发送请求,然后读取Response
流,我得到了我的 asp 页面的内容以及我写入的数据Response.OutputStream.Write(info, 0, info.Length)
。
就我而言,我将字符串“1”写为字节,客户端程序的输出是:
1<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><title>
</title></head>
<body>
<form method="post" action="LauncherLogin.aspx" id="form1">
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwULLTE2MTY2ODcyMjlkZMraZTexYtyyQ9NaNN0YMvYIep5peaSEIrDBqxTff6rW" />
<div>
</div>
</form>
</body>
</html>
我不希望收到的响应中有这个 HTML,我该如何摆脱它?
客户端代码(c#程序):
public static string SendInformation(string values)
{
ASCIIEncoding encoding = new ASCIIEncoding();
string postData = values;
byte[] data = encoding.GetBytes(postData);
// Prepare web request...
HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(serverAddress + commmunicationForm);
myRequest.Method = "POST";
myRequest.ContentType = "text/html";
myRequest.ContentLength = data.Length;
string result;
using (Stream stream = myRequest.GetRequestStream())
{
stream.Write(data, 0, data.Length);
}
using (WebResponse response = myRequest.GetResponse())
{
using (var reader = new StreamReader(response.GetResponseStream()))
{
result = reader.ReadToEnd();
return result;
}
}
}
ASP.aspx.cs 代码(服务器端):
if (dt.Rows.Count > 0)
{
Response.Clear();
Response.ClearContent();
Response.ClearHeaders();
Response.Buffer = false;
string errNum = dt.Rows[0][0].ToString();
byte[] info = Encoding.ASCII.GetBytes(errNum);
Response.OutputStream.Write(info, 0, info.Length);
Response.Flush();
}
即使我这样做:
string errNum = dt.Rows[0][0].ToString();
byte[] info = Encoding.ASCII.GetBytes(errNum);
Response.OutputStream.Write(info, 0, info.Length);
Response.Flush();
我仍然得到 html 代码:|
我确实尝试了Response.Clear()
其他.Clear()
方法,但没有成功。