Java URLEncoder 是一個支援 HTML 表單編碼的實用程式類別。使用 Java 的 URLEncoder 類別實用程序,HTML 的形式變得更加可靠和穩定。每當使用者呼叫 get 方法時,編碼器都會在 URL 末尾附加特殊字元、值和參數,這使得 URL 在某種程度上未經身份驗證。此外,該值還使用特殊字符,進一步僅使用 HTML 來執行所有操作的順利處理。當 Java URLEncoder 及其定義的實用程式類別存在時,完全依賴 HTML 根本不是一個好習慣。
開始您的免費軟體開發課程
網頁開發、程式語言、軟體測試及其他
文法:
public static String encode(String st, String enc1)throws UnsupportedEncodingException
語法流程的參數如下:
URLEncoder 是用於任何 HTML 編碼的 Java 類別的實用程式。當 URLEncoder 的 Java 實用程式類別的存在可以順利地增強字串轉換的活動類型時,每次使用 HTML 編碼,即使對於小型和簡化的方法調用,也是一種不需要的活動。
當涉及字串及其從字元解析和編碼、解碼特殊字串因素的轉換時,它被認為是最安全可靠的實用程式類別之一。此外,它還利用內建功能,該功能廣泛用於將字串轉換為必要的格式,然後在使用URLEncoder 對字串進行編碼時應用於該字串的一些行為準則或規則,如下所示:
一個範例將闡明字串編碼需要遵循 UTF 格式標準,這表示如果我們有一些參數或值包含一些特殊字元和空格的值,則透過範例進行示範:
以下是java urlencoder的範例:
This program is used to illustrate the URLEncoder utility of Java where the input string is given as the base url for the link and then a string query for retrieving the final string using UTF-8 as a conventional standard for encoding. Output is shown where one encoded string is without URL and the other with UTF-8 standard, which comprises the URL.
Code:
import java.io.UnsupportedEncodingException; import java.net.MalformedURLException; import java.net.URL; import java.net.URLEncoder; public class UrlEncoderJava { public static void main(String[] args) throws MalformedURLException, UnsupportedEncodingException { String baseurl = "https://www.educba.com/?q="; String query = "u@educba for educba"; System.out.println("Without encoding URL :"); URL url = new URL(baseurl + query); System.out.println(url); System.out.println("URL after encoding :"); url = new URL(baseurl + URLEncoder.encode(query, "UTF-8")); System.out.println(url); } }
Output:
This program is used to represent the encoded string which makes use of the standard Charsets of the UTF_8 to the string and then provides the entire encoded string as shown in the output after converting the URL link with the defined standard and Encoder class of java.
Code:
import java.net.URLEncoder; import java.nio.charset.StandardCharsets; import java.io.UnsupportedEncodingException; public class URLEncodingParsing { private static String encodingOfValue(String value) { try { return URLEncoder.encode(value, StandardCharsets.UTF_8.toString()); } catch (UnsupportedEncodingException ex) { throw new RuntimeException(ex.getCause()); } } public static void main(String[] args) { String baseUrl = "https://www.educba.com/search?q="; String query = "educba@Java@lang"; String encodedQuery = encodingOfValue(query); String completeUrl = baseUrl + encodedQuery; System.out.println(completeUrl); } }
Output:
URLEncoder in java is a utility class that provides aid for the HTML related forms to encode the special characters being provided for parsing. The UTF-8 standard recommended by W#C has enhanced the overall encoding method for encoding and conversion of the encoded string to the final string after parsing. Using this class for string encoding is a very reliable and secured form of coding; as always, making HTML is not preferred.
以上是Java URL編碼器的詳細內容。更多資訊請關注PHP中文網其他相關文章!