zoukankan      html  css  js  c++  java
  • xmlHttpRequest 以Post方式发数据到Asp.net页,在gb2312编码下的解决办法

       首先xmlHttpRequest 使用Post时,需要对数据进行编码,在客户端一般使用js中的encodeURIComponent

     在web.config中指定了gb2312编码后,在aspx页面中如果直接使用 Request[xxx]那么结果将会出现乱码,

    原因是asp.net系统使用gb2312编码对上传的数据进行解码还原,而encodeURIComponent编码是按uft-8来的.

     为了避免这个问题,我们需要见xmlHttpRequest发送上来的原始数据(字节)按utf-8进行解码处理,

    方式一

     代码如下

     浏览器端(js):

    function myXMLHttpRequest(){
     var xmlHttp = false;
     try {
      xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
     } catch (e) {
      try {
       xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (e2) {
       xmlHttp = false;
      }
     }
     if (!xmlHttp && typeof XMLHttpRequest != 'undefined') {
      xmlHttp = new XMLHttpRequest();
     }
     return xmlHttp;
    }

     var xmlHttp=myXMLHttpRequest();
      content = "user="+encodeURIComponent("大发啊个啊按时法米]]吗吗吗*-234^342942023&^+\\");
      content +="&data=" +encodeURIComponent("到这里结束了!");
      xmlHttp.Open("POST", "doc.aspx", false);
      xmlHttp.setRequestHeader("Content-Length",content.length);
      xmlHttp.setRequestHeader("CONTENT-TYPE","application/x-www-form-urlencoded");
      xmlHttp.Send(content);

     服务器端(asp.net)

            Byte[] bytes= Request.BinaryRead(Request.ContentLength);
            NameValueCollection req= FillFromEncodedBytes(bytes, Encoding.UTF8);
            Response.Write(req["User"]);

    其中FillFromEncodedBytes定义如下(这个是ms内部代码,通过reflector 获得,作用就是根据url编码将字段跟数据分解出来)

    略有改动

        private NameValueCollection FillFromEncodedBytes(byte[] bytes, Encoding encoding)
        {
            NameValueCollection _form = new NameValueCollection();
           
            int num = (bytes != null) ? bytes.Length : 0;
            for (int i = 0; i < num; i++)
            {
                string str;
                string str2;
                int offset = i;
                int num4 = -1;
                while (i < num)
                {
                    byte num5 = bytes[i];
                    if (num5 == 0x3d)
                    {
                        if (num4 < 0)
                        {
                            num4 = i;
                        }
                    }
                    else if (num5 == 0x26)
                    {
                        break;
                    }
                    i++;
                }
                if (num4 >= 0)
                {
                    str = HttpUtility.UrlDecode(bytes, offset, num4 - offset, encoding);
                    str2 = HttpUtility.UrlDecode(bytes, num4 + 1, (i - num4) - 1, encoding);
                }
                else
                {
                    str = null;
                    str2 = HttpUtility.UrlDecode(bytes, offset, i - offset, encoding);
                }
                _form.Add(str, str2);
                if ((i == (num - 1)) && (bytes[i] == 0x26))
                {
                    _form.Add(null, string.Empty);
                }
            }
            return _form;
        }

     参考代码:https://files.cnblogs.com/wdfrog/xmlhttp.rar

    ---------------------------------------------------------
    方式二,使用GET方式发来的请求,上面(方式一)的每个字段也可以使用下面方式进行转换
    客户端:(使用Utf-8编码方式)
    /services/regServices.aspx?username='+encodeURI(un.value)
    服务端:(默认情况下,Request["xxx"]使用gb2312进行UrlDecode处理,所以将结果按gb2312 UrlEncode,后再使用utf-8进行UrlDecode)
        string data1 = Util.GetQ("username", "");
            string data2= HttpUtility.UrlEncode(data1, Encoding.GetEncoding("GB2312"));
            string username = HttpUtility.UrlDecode(data2, Encoding.UTF8);

    方式三,处理方式类似第一种,因为要获取的Get,中的Query

            IServiceProvider provider = (IServiceProvider)HttpContext.Current;
            HttpWorkerRequest wr= provider.GetService(typeof(HttpWorkerRequest)) as HttpWorkerRequest;
            byte[] bytes = wr.GetQueryStringRawBytes();
            NameValueCollection req = FillFromEncodedBytes(bytes, Encoding.UTF8);
            string u = req["username"];

  • 相关阅读:
    convert image to base64 and post to RESTful wcf
    在android webview实现截屏的手动tounchmove裁剪图片
    How to use jquery ajax and android request security RESTful WCF
    using swfUpload in asp.net mvc
    using HttpClient and sending json data to RESTful server in adroind
    ODP.NET数据访问
    android image watermark
    解决国内不能访问github的问题
    idapro权威指南第二版阅读笔记第九章 交叉引用和绘图功能
    idapro权威指南第二版阅读笔记第二章 逆向和反汇编工具
  • 原文地址:https://www.cnblogs.com/wdfrog/p/1315750.html
Copyright © 2011-2022 走看看