Encode encoding of URLs when an interface is called

Source: Internet
Author: User

First, the URL is utf-8 encoded

1) In the interface call may encounter the need to encode and decode the URL problem, on the network to find a corresponding method to provide reference

two, the encoding and decoding tool class found on the network
 PackageCom.rain.demo;Importjava.io.UnsupportedEncodingException; Public classutf8{/*** Utf8url Code *@paramS *@return   */   Public Static Finalstring Utf8urlencode (string text) {StringBuffer result=NewStringBuffer ();  for(inti = 0; I < text.length (); i++) {      Charc =Text.charat (i); if(c >= 0 && C <= 255) {result.append (c); }Else {        byte[] B =New byte[0]; Try{b= Character.tostring (c). GetBytes ("UTF-8"); }Catch(Exception ex) {} for(intj = 0; J < B.length; J + +) {          intK =B[j]; if(k < 0) K + = 256; Result.append ("%" +integer.tohexstring (k). toUpperCase ()); }      }    }    returnresult.tostring (); }  /*** Utf8url decoding *@paramtext *@return   */   Public Static Finalstring Utf8urldecode (String text) {string result= ""; intp = 0; if(text!=NULL&& text.length () >0) {text=text.tolowercase (); P= Text.indexof ("%e"); if(p = =-1)returntext;  while(P! =-1) {result+ = text.substring (0, p); Text=text.substring (P, text.length ()); if(Text = = "" | | Text.length () < 9)returnresult; Result+ = Codetoword (text.substring (0, 9)); Text= Text.substring (9, Text.length ()); P= Text.indexof ("%e"); }    }    returnResult +text; }  /*** Utf8url encoded to characters *@paramtext *@return   */  Private Static Finalstring Codetoword (String text) {string result; if(Utf8codecheck (text)) {byte[] Code =New byte[3]; code[0] = (byte) (Integer.parseint (text.substring (1, 3), 16)-256); code[1] = (byte) (Integer.parseint (Text.substring (4, 6), 16)-256); code[2] = (byte) (Integer.parseint (Text.substring (7, 9), 16)-256); Try{result=NewString (Code, "UTF-8"); }Catch(Unsupportedencodingexception ex) {result=NULL; }    }    Else{result=text; }    returnresult; }  /*** Encoding is valid *@paramtext *@return   */  Private Static Final BooleanUtf8codecheck (String text) {string sign= ""; if(Text.startswith ("%e"))       for(inti = 0, p = 0; P! =-1; i++) {p= Text.indexof ("%", p); if(P! =-1) P++; Sign+=p; }    returnSign.equals ("147-1"); }  /*** Determine if utf8url code *@paramtext *@return   */   Public Static Final BooleanIsutf8url (String text) {text=text.tolowercase (); intp = text.indexof ("%"); if(P! =-1 && text.length ()-p > 9) {text= Text.substring (p, p + 9); }    returnUtf8codecheck (text); }  /*** Test *@paramargs*/   Public Static voidMain (string[] args) {String URL; URL= "Http://www.google.com/search?hl=zh-CN&newwindow=1&q=%E4%B8%AD%E5%9B%BD%E5%A4%A7%E7%99%BE%E7%A7%91%E5 %9c%a8%e7%ba%bf%e5%85%a8%e6%96%87%e6%a3%80%e7%b4%a2&btng=%e6%90%9c%e7%b4%a2&lr= "; if(Chartools.isutf8url (URL)) {System.out.println (Chartools.utf8urldecode (URL)); } URL= "http://www.google.com/search?hl=zh-cn&newwindow=1&q= China Encyclopedia Online Full-Text search &btng= &lr="; if(!chartools.isutf8url (URL))    {System.out.println (Chartools.utf8urlencode (URL)); }  }}

Encode encoding of URLs when an interface is called

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.