Java URI getRawQuery()方法及示例

来源:这里教程网 时间:2026-02-17 21:15:34 作者:

Java URI getRawQuery()方法及示例

getRawQuery()函数是URI类的一部分。getRawQuery()函数返回指定URI的原始查询。该函数返回查询的准确值,如果有转义八位数的序列,则无需解码。

函数签名:

public String getRawQuery()

语法:

uri.getRawQuery()

参数: 该函数不需要任何参数

返回类型: 该函数返回字符串类型

下面的程序说明了getRawQuery()函数的使用。

例1: 给定一个URI,我们将使用getRawQuery()函数获得原始查询。

// Java program to show the // use of the function getRawQuery()  import java.net.*;  class Solution {    public static void main(String args[])    {        // uri  object        URI uri = null;          try {            // create a URI            uri = new URI("https://www.geeksforgeeks.org/url-getprotocol-method-in-java-with-examples?title=protocol");              // get the  Query            String Raw_Query = uri.getRawQuery();              // display the URL            System.out.println("URI = " + uri);              // display the  Raw Query            System.out.println(" Raw Query=" + Raw_Query);        }        // if any error occurs        catch (URISyntaxException e) {            // display the error            System.out.println("URI Exception =" + e.getMessage());        }    }}

输出:

URI = https://www.geeksforgeeks.org/url-getprotocol-method-in-java-with-examples?title=protocol Raw Query=title=protocol

例2: 现在我们将不提供任何查询,而使用函数来获取查询并查看结果。

// Java program to show the // use of the function getRawQuery()  import java.net.*;  class Solution {    public static void main(String args[])    {        // uri  object        URI uri = null;          try {            // create a URI            uri = new URI("https://www.geeksforgeeks.org/url-getprotocol-method-in-java-with-examples");              // get the  Query            String Raw_Query = uri.getRawQuery();              // display the URL            System.out.println("URI = " + uri);              // display the  Raw Query            System.out.println(" Raw Query=" + Raw_Query);        }        // if any error occurs        catch (URISyntaxException e) {            // display the error            System.out.println("URI Exception =" + e.getMessage());        }    }}

输出:

URI = https://www.geeksforgeeks.org/url-getprotocol-method-in-java-with-examples Raw Query=null

例3: getQuery()和getRawQuery()返回的值是一样的,除了所有转义的八位数序列被解码。getRawPath()返回用户提供的字符串的准确值,但getQuery()函数对转义八位字节的序列(如果有)进行解码。

// Java program to show the // use of the function getRawQuery()  import java.net.*;  class Solution {    public static void main(String args[])    {        // uri  object        URI uri = null;          try {            // create a URI            uri = new URI("https://www.geeksforgeeks.org/url-getprotocol-method-in-java-with-examples?title=protocol%E2%82%AC");              // get the  Query            String _Query = uri.getQuery();              // display the URL            System.out.println("URI = " + uri);              // display the  Query            System.out.println(" Query=" + _Query);              // get the  Raw Query            String Raw_Query = uri.getRawQuery();              // display the Raw Query            System.out.println(" Raw Query=" + Raw_Query);        }        // if any error occurs        catch (URISyntaxException e) {            // display the error            System.out.println("URI Exception =" + e.getMessage());        }    }}

输出:

URI = https://www.geeksforgeeks.org/url-getprotocol-method-in-java-with-examples?title=protocol%E2%82%AC Query=title=protocol? Raw Query=title=protocol%E2%82%AC

相关推荐