asp.net url傳遞中文亂碼的解決方案

來源:互聯網
上載者:User

asp教程.net url傳遞中文亂碼的解決方案

1.設定web.config檔案。
<system.web>
......
<globalization requestencoding="gb2312" responseencoding="gb2312" culture="zh-cn" fileencoding="gb2312" />
......
</system.web>
2.傳遞中文之前,將要傳遞的中文參數進行編碼,在接收時再進行解碼。
>> 進行傳遞
string name = "中文參數";
response.redirect("b.aspx?name="+server.urlencode(name));
>> 進行接收
string name = request.querystring["name"];
response.write(server.urldecode(name));
3.如果是從 .html 檔案向 .aspx 檔案進行傳遞中文參數的話(即不從後台用 redirect()方法進行 url 轉換)。一樣要將傳遞的中文參數進行編碼,在接收時再進行解碼。
>> 進行傳遞
<script language="網頁特效">
function gourl()
{
var name = "中文參數";
location.href = "b.aspx?name="+escape(name);
}
</script>
<body onclick="gourl()">
>> 進行接收
string name = request.querystring["name"];
response.write(server.urldecode(name));


執行個體方法

a項目中
        string url = "http://" + arr[2] + ":111/loginif.aspx?uid=" + server.urlencode(arr[0]) + "&pwd=" + arr[1];   //中文帳號轉碼

        clientscript.registerclientscriptblock(this.gettype(), "this", "<script>window.open('" + url + "')</script>"); 這樣傳參數 uid傳的是中文

b項目裡接收 
 userloginif(server.urlencode(request["uid"].tostring()), request["pwd"].tostring());   //中文必須轉碼


指定編碼
發送方 server.urlencode("中文", encoding.getencoding("gb2312"));
接收方 server.urldecode(request["key"], encoding.getencoding("gb2312"));


userloginif(server.urldecode(request["uid"].tostring()), request["pwd"].tostring());  


encode是編碼,decode是解碼.

如果還不行,
就使用post方式,或使用requestheader,或
考慮用webservice做proxy轉寄.

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.