Asp對URL編碼和解碼

來源:互聯網
上載者:User

URL編碼是指為了將資訊通過URL進行傳輸,所以必須將某些含有特殊意義的字元進行替換的一種編碼方式,在asp中我們都知道有一個Server.URLEncode的函數可以完成這個功能。即:
  如果有空格就用%20代替,如果有其它字元就用%ASCII代替,如果有漢字等四個位元組的字元,就用兩個%ASCII來代替。不過有時候我們也需要將經過這種編碼的字串進行解碼,但asp並沒有提供相關的函數,這給我們處理問題帶來了一定的麻煩。其實我們只要知道了編碼規則後,就可以用asp代碼來實現我們自己的URlDecode函數了。
  具體實現如下:
function urldecode(encodestr)<br />newstr=""<br />havechar=false<br />lastchar=""<br />for i=1 to len(encodestr)<br />char_c=mid(encodestr,i,1)<br />if char_c="+" then<br />newstr=newstr & " "<br />elseif char_c="%" then<br />next_1_c=mid(encodestr,i+1,2)<br />next_1_num=cint("&H" & next_1_c)<br />if havechar then<br />havechar=false<br />newstr=newstr & chr(cint("&H" & lastchar & next_1_c))<br />else<br />if abs(next_1_num)<=127 then<br />newstr=newstr & chr(next_1_num)<br />else<br />havechar=true<br />lastchar=next_1_c<br />end if<br />end if<br />i=i+2<br />else<br />newstr=newstr & char_c<br />end if<br />next<br />urldecode=newstr<br />end function<br />

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.