zoukankan      html  css  js  c++  java
  • 请求的值。从客户端中检测到有潜在危险的 Request.Form 值

    旧方法:
     
    wef.config里加上节点
    <httpRuntime requestValidationMode="2.0" />

    下面可选
     validateRequest="false"
     
    新方法:
     

    一:前端对富文本字符串进行encodeURI编码,服务端进行HttpUtility.UrlDecode解码操作

    前端:

    var str = '<p><span style="color: #00B0F0;"><em><strong>我想留在你的身边,</strong></em></span><br/></p><p><span style="color: #7030A0;"><strong><span style="text-decoration: underline;">深情款款多么可怜;</span></strong></span></p>';
        $(function() {
            $.ajax({
                type: "post",
                url: "TestHandle.ashx",
                data: { Title: 'jack', Content: encodeURI(str) },
                success: function (data) {
                    $("#div").html(data);
                }
            });
        });

    后端:

    public void ProcessRequest(HttpContext context)
        {
            string str = context.Request["content"];
            string content = HttpUtility.UrlDecode(str);
            context.Response.ContentType = "text/plain";
            context.Response.Write(content);
        }

    二:前端不以form的方式提交,直接以json方式提交,服务端从request的body中读取数据,然后反序列化,得到信息

    前端:

    var str = '<p><span style="color: #00B0F0;"><em><strong>我想留在你的身边,</strong></em></span><br/></p><p><span style="color: #7030A0;"><strong><span style="text-decoration: underline;">深情款款多么可怜;</span></strong></span></p>';
        var temp = { Title: 'jack', Content: str };
        $.ajax({
            type: "post",
            url: "TestHandle.ashx",
            contentType:"application/json;charset=utf-8",
            data: JSON.stringify(temp),
            success: function (data) {
                $("#div").html(data);
            }
        });

    后端:

    string bodyText;
        using (var bodyReader = new System.IO.StreamReader(context.Request.InputStream))
        {
            bodyText = bodyReader.ReadToEnd();
        }
        dynamic bodyObj = JsonConvert.DeserializeObject(bodyText);
    
        context.Response.ContentType = "text/plain";
        context.Response.Write(bodyObj.Content);
  • 相关阅读:
    Mysql备份和恢复
    前端Css学习
    jQuery学习
    HTML页面学习
    Linux下java环境变量配置
    windows下java环境变量标准配置
    oracle查询消耗服务器资源SQL语句
    Java主线程在子线程执行完毕后再执行
    CentOS7 安装 Redis
    查看Oracle表空间使用情况
  • 原文地址:https://www.cnblogs.com/wybshyy/p/13783795.html
Copyright © 2011-2022 走看看