您的位置:首页 > 理论基础 > 计算机网络

xmlHttpRequest 以Post方式发数据到Asp.net页,在gb2312编码下的解决办法

2008-10-21 11:30 621 查看
 

   首先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;

    }

 参考代码:http://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"];

 

google_ad_client = "pub-9768854185654179";
/* 468x60, 创建于 09-2-12 */
google_ad_slot = "6840815711";
google_ad_width = 468;
google_ad_height = 60;

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: