Java string to urlencode

Java string to urlencode

For example using UTF-8 as the encoding scheme the string «The string ü@foo-bar» would get converted to «The+string+%C3%BC%40foo-bar» because in UTF-8 the character ü is encoded as two bytes C3 (hex) and BC (hex), and the character @ is encoded as one byte 40 (hex).

Method Summary

Methods declared in class java.lang.Object

Method Detail

encode

@Deprecated public static String encode​(String s)

The resulting string may vary depending on the platform’s default encoding. Instead, use the encode(String,String) method to specify the encoding.

Translates a string into x-www-form-urlencoded format. This method uses the platform’s default encoding as the encoding scheme to obtain the bytes for unsafe characters.

encode

public static String encode​(String s, String enc) throws UnsupportedEncodingException

Translates a string into application/x-www-form-urlencoded format using a specific encoding scheme. This method behaves the same as encode(java.lang.String,java.nio.charset.Charset) except that it will look up the charset using the given encoding name.

encode

public static String encode​(String s, Charset charset)

Translates a string into application/x-www-form-urlencoded format using a specific Charset. This method uses the supplied charset to obtain the bytes for unsafe characters. Note: The World Wide Web Consortium Recommendation states that UTF-8 should be used. Not doing so may introduce incompatibilities.

Report a bug or suggest an enhancement
For further API reference and developer documentation see the Java SE Documentation, which contains more detailed, developer-targeted descriptions with conceptual overviews, definitions of terms, workarounds, and working code examples.
Java is a trademark or registered trademark of Oracle and/or its affiliates in the US and other countries.
Copyright © 1993, 2023, Oracle and/or its affiliates, 500 Oracle Parkway, Redwood Shores, CA 94065 USA.
All rights reserved. Use is subject to license terms and the documentation redistribution policy.

Источник

Class URLEncoder

Utility class for HTML form encoding. This class contains static methods for converting a String to the application/x-www-form-urlencoded MIME format. For more information about HTML form encoding, consult the HTML specification.

  • The alphanumeric characters » a » through » z «, » A » through » Z » and » 0 » through » 9 » remain the same.
  • The special characters » . «, » — «, » * «, and » _ » remain the same.
  • The space character » » is converted into a plus sign » + «.
  • All other characters are unsafe and are first converted into one or more bytes using some encoding scheme. Then each byte is represented by the 3-character string » %xy «, where xy is the two-digit hexadecimal representation of the byte. The recommended encoding scheme to use is UTF-8. However, for compatibility reasons, if an encoding is not specified, then the default charset is used.
Читайте также:  Php static file server

For example using UTF-8 as the encoding scheme the string «The string ü@foo-bar» would get converted to «The+string+%C3%BC%40foo-bar» because in UTF-8 the character ü is encoded as two bytes C3 (hex) and BC (hex), and the character @ is encoded as one byte 40 (hex).

Method Summary

Methods declared in class java.lang.Object

Method Details

encode

The resulting string may vary depending on the default charset. Instead, use the encode(String,String) method to specify the encoding.

Translates a string into x-www-form-urlencoded format. This method uses the default charset as the encoding scheme to obtain the bytes for unsafe characters.

encode

Translates a string into application/x-www-form-urlencoded format using a specific encoding scheme. This method behaves the same as encode(String s, Charset charset) except that it will look up the charset using the given encoding name.

encode

Translates a string into application/x-www-form-urlencoded format using a specific Charset. This method uses the supplied charset to obtain the bytes for unsafe characters. Note: The World Wide Web Consortium Recommendation states that UTF-8 should be used. Not doing so may introduce incompatibilities.

Report a bug or suggest an enhancement
For further API reference and developer documentation see the Java SE Documentation, which contains more detailed, developer-targeted descriptions with conceptual overviews, definitions of terms, workarounds, and working code examples. Other versions.
Java is a trademark or registered trademark of Oracle and/or its affiliates in the US and other countries.
Copyright © 1993, 2023, Oracle and/or its affiliates, 500 Oracle Parkway, Redwood Shores, CA 94065 USA.
All rights reserved. Use is subject to license terms and the documentation redistribution policy.

Источник

Class URLEncoder

Utility class for HTML form encoding. This class contains static methods for converting a String to the application/x-www-form-urlencoded MIME format. For more information about HTML form encoding, consult the HTML specification.

  • The alphanumeric characters » a » through » z «, » A » through » Z » and » 0 » through » 9 » remain the same.
  • The special characters » . «, » — «, » * «, and » _ » remain the same.
  • The space character » » is converted into a plus sign » + «.
  • All other characters are unsafe and are first converted into one or more bytes using some encoding scheme. Then each byte is represented by the 3-character string » %xy «, where xy is the two-digit hexadecimal representation of the byte. The recommended encoding scheme to use is UTF-8. However, for compatibility reasons, if an encoding is not specified, then the default encoding of the platform is used.
Читайте также:  Создать виртуальную среду python vs code

For example using UTF-8 as the encoding scheme the string «The string ü@foo-bar» would get converted to «The+string+%C3%BC%40foo-bar» because in UTF-8 the character ü is encoded as two bytes C3 (hex) and BC (hex), and the character @ is encoded as one byte 40 (hex).

Method Summary

Methods declared in class java.lang.Object

Method Details

encode

The resulting string may vary depending on the platform’s default encoding. Instead, use the encode(String,String) method to specify the encoding.

Translates a string into x-www-form-urlencoded format. This method uses the platform’s default encoding as the encoding scheme to obtain the bytes for unsafe characters.

encode

Translates a string into application/x-www-form-urlencoded format using a specific encoding scheme. This method behaves the same as encode(String s, Charset charset) except that it will look up the charset using the given encoding name.

encode

Translates a string into application/x-www-form-urlencoded format using a specific Charset. This method uses the supplied charset to obtain the bytes for unsafe characters. Note: The World Wide Web Consortium Recommendation states that UTF-8 should be used. Not doing so may introduce incompatibilities.

Report a bug or suggest an enhancement
For further API reference and developer documentation see the Java SE Documentation, which contains more detailed, developer-targeted descriptions with conceptual overviews, definitions of terms, workarounds, and working code examples. Other versions.
Java is a trademark or registered trademark of Oracle and/or its affiliates in the US and other countries.
Copyright © 1993, 2023, Oracle and/or its affiliates, 500 Oracle Parkway, Redwood Shores, CA 94065 USA.
All rights reserved. Use is subject to license terms and the documentation redistribution policy.

Источник

How to URL Encode a String in Java

How to do URL Encoding in Java

Programmers often need to perform URL encoding on query strings or form parameters while calling a remote api. URL encoding makes the transmitted data more reliable and secure.

URL Encoding a Query string or Form parameter in Java

Java provides a URLEncoder class for encoding any query string or form parameter into URL encoded format. The following example demonstrates how to use URLEncoder.encode() method to perform URL encoding in Java.

Pitfall to avoid: Do not encode the entire URL. Encode only the query string and path segment.

import java.net.URLEncoder; import java.nio.charset.StandardCharsets; import java.io.UnsupportedEncodingException; class URLEncodingExample  // Method to encode a string value using `UTF-8` encoding scheme private static String encodeValue(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.google.com/search?q="; String query = "Hellö Wörld@Java"; String encodedQuery = encodeValue(query); // Encoding a query string String completeUrl = baseUrl + encodedQuery; System.out.println(completeUrl); > >
# Output https://www.google.com/search?q=Hell%C3%B6+W%C3%B6rld%40Java

Note that Java’s URLEncoder class encodes space character( » » ) into a + sign. This is contrary to other languages like Javascript that encode space character into %20 .

Check out this StackOverflow discussion to learn what it means to encode the space character into %20 or + sign.

The encode() function takes the encoding to be used as the second parameter. In our example, we’re using UTF-8 encoding scheme. The world wide web consortium recommends that you use UTF-8 encoding scheme whenever possible to avoid incompatibilities.

An UnsupportedEncodingException is thrown if the encoding is not supported.

Источник

Java string to urlencode

The resulting string may vary depending on the platform’s default encoding. Instead, use the encode(String,String) method to specify the encoding.

Methods inherited from class java.lang.Object

Method Detail

encode

@Deprecated public static String encode(String s)

Deprecated. The resulting string may vary depending on the platform’s default encoding. Instead, use the encode(String,String) method to specify the encoding.

Translates a string into x-www-form-urlencoded format. This method uses the platform’s default encoding as the encoding scheme to obtain the bytes for unsafe characters.

encode

public static String encode(String s, String enc) throws UnsupportedEncodingException

Translates a string into application/x-www-form-urlencoded format using a specific encoding scheme. This method uses the supplied encoding scheme to obtain the bytes for unsafe characters. Note: The World Wide Web Consortium Recommendation states that UTF-8 should be used. Not doing so may introduce incompatibilities.

Submit a bug or feature
For further API reference and developer documentation, see Java SE Documentation. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples.
Copyright © 1993, 2023, Oracle and/or its affiliates. All rights reserved. Use is subject to license terms. Also see the documentation redistribution policy.

Источник

Оцените статью