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中文网其他相关文章!