C# 潜在危险的请求。从客户端检测到表单值(
可能重复:C# 潜在危险的请求。从客户端检测到表单值(,c#,asp.net,C#,Asp.net,可能重复: 我收到错误消息: A potentially dangerous Request.Form value was detected from the client (ctl00$MainContent$textboxError=... 在运行asp.net(C#)web应用程序之后。如何修复它 这是我的密码 using System; using System.Data; using System.Configuration; using System.Web; using
我收到错误消息:
A potentially dangerous Request.Form value was detected from the client
(ctl00$MainContent$textboxError=...
在运行asp.net(C#)web应用程序之后。如何修复它 这是我的密码
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Net;
using System.Text.RegularExpressions;
namespace SMS
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
textboxRecipient.Width = 400;
textboxMessage.Width = 450;
textboxMessage.Rows = 10;
textboxError.Width = 400;
textboxError.Rows = 5;
textboxError.ForeColor = System.Drawing.Color.Red;
textboxError.Visible = false;
textboxError.Text = "";
if (!Page.IsPostBack)
{
textboxRecipient.Text = "+85593840396";
textboxMessage.Text = "Hello World!";
}
}
protected void buttonSendOnClick(object sender, EventArgs e)
{
if (textboxRecipient.Text == "")
{
textboxError.Text += "Recipient(s) field must not be empty!\n";
textboxError.Visible = true;
return;
}
string ozSURL = "http://127.0.0.1"; //where Ozeki NG SMS Gateway is running
string ozSPort = "9501"; //port number where Ozeki NG SMS Gateway is listening
string ozUser = HttpUtility.UrlEncode("tenh"); //username for successful login
string ozPassw = HttpUtility.UrlEncode("tenh123"); //user's password
string ozMessageType = "SMS:TEXT"; //type of message
string ozRecipients = HttpUtility.UrlEncode(textboxRecipient.Text);
string ozMessageData = HttpUtility.UrlEncode(textboxMessage.Text);
string createdURL = ozSURL + ":" + ozSPort + "/httpapi" +
"?action=sendMessage" +
"&username=" + ozUser +
"&password=" + ozPassw +
"&messageType=" + ozMessageType +
"&recipient=" + ozRecipients +
"&messageData=" + ozMessageData;
try
{
HttpWebRequest myReq = (HttpWebRequest)WebRequest.Create(createdURL);
HttpWebResponse myResp = (HttpWebResponse)myReq.GetResponse();
System.IO.StreamReader respStreamReader=new
System.IO.StreamReader(myResp.GetResponseStream());
string responseString = respStreamReader.ReadToEnd();
respStreamReader.Close();
myResp.Close();
//inform the user
textboxError.Text = responseString;
textboxError.Visible = true;
}
catch (Exception)
{
textboxError.Text = "Ozeki NG SMS Gateway Server is not running!";
textboxError.Visible = true;
}
}
}
}
我已将以下代码添加到我的web.config:
validateRequest="false" and requestValidationMode="2.0"
不知道为什么
validateRequest=“false”
不起作用。错误原因:因为您从包含Html标记的网页获取数据,而textbox text属性不允许为其分配Html字符串,所以您必须使用将Html标记转换为等效代码的方法。
将responseString
分配给textboxError.Text
时使用方法。此方法将潜在的不安全字符转换为其HTML编码的等效字符
textboxError.Text = Server.HTMLEncode(responseString);
重复此:、此:和无数其他问题。您已经问了3次相同的问题。为什么您要分别问3次相同的问题?@SimonWhitehead我也知道这是第三次,但其他问题也没有公认的答案和正确的解决方案。是否每次单击“buttonSend”按钮?第一次也不起作用?当我第二次单击按钮Send时会出现此错误time@Shree卡纳尔:我问这个问题是因为他们的答案不适用。谢谢你的好答案。但是在我使用textboxError.Text=Server.HTMLEncode(responseString)之后;我得到的错误代码如下:Response0ActionSendMessage/ActionDataAcceptReportStatusCode0/StatusCodeStatusTextMessage accepted for delivery/StatusTextMessageID03a22ae5-e604-4fb6-9670-336294905a9d/MessageIDRecipient+85593840396我只在textboxError.Text=Server.HTMLEncode(responseString)上使用了HTML.Encode;此方法将html标记转换为相应的代码。请参阅msdn文档。您也可以使用Regex删除html标记。
String result=Regex.Replace(responseString,@“]*>,String.Empty);
有关详细信息,您也可以使用asp:literal控件而不是像这样的文本
,这样您就不必使用Html.Encode方法或rejex来删除Html。请在msdni used String result=Regex.Replace(responseString,@“]*>,String.Empty)上阅读更多信息;若要在收到此错误后删除html页,请执行以下操作:sendMessage0Message accepted for Delivery 75D9C6F1-515f-43f6-9b7b-5c2482ce95e8+85568922903。出现了什么问题?它们没有说明行。单击“发送”按钮时,会发出此错误警报