Java URLDecoder Methods - Java 147

Java URLDecoder Methods – Java 147

Java URLDecoder Methods

用於對 URL 統一資源定位,進行解碼的工具類別,當使用者透過 get 方法請求特定網站時,表單參數及其值將會新增在 ? 之後,當在未解釋的值中使用特殊字元時,就會出現問題,因此為了明確地對 URL 進行編碼,於是需要將其解碼,取得原本的值,提供了靜態方法,依指定編碼的方案,將字串解碼, URLDecoder Java Methods 本篇增加了範例,並透過單元測試來驗證產出結果。

檔案目錄

./
   +- src
       +- test
       |   +- org
       |       +- ruoxue
       |           +- java_147
       |               +- net
       |                   +- URLDecoderMethodsTest.java  

單元測試

URLDecoder Methods Java 提供 URLDecoder 解碼器操作,指定編碼方案,如: UTF-8 ,將字串解碼。

decode

建立一個 String ,使用 URLDecoder 指定編碼方案,如: UTF-8 ,進行解碼。

	@Test
	public void decode() {
		try {
			String value = "https%3A%2F%2Fwww.ruoxue.org";
			System.out.println(value);
			String result = URLDecoder.decode(value, StandardCharsets.UTF_8.toString());
			System.out.println(result);
			assertThat(result).isEqualTo("https://www.ruoxue.org");
		} catch (UnsupportedEncodingException ex) {
			throw new RuntimeException(ex.getMessage(), ex);
		}
	}
https%3A%2F%2Fwww.ruoxue.org
https://www.ruoxue.org

decodeQuery

URLDecoder Methods Java 建立一個 String ,使用 URLDecoder 指定編碼方案,如: UTF-8 ,進行解碼。

	@Test
	public void decodeQuery() {
		try {
			String value = "https://www.ruoxue.org/?s=name+%25";
			System.out.println(value);
			URI uri = new URI(value);
			String query = uri.getRawQuery();
			System.out.println(query);
			String[] array = query.split("=");
			String result = URLDecoder.decode(array[1], StandardCharsets.UTF_8.toString());
			System.out.println(result);
			assertThat(result).isEqualTo("name %");
		} catch (Exception ex) {
			throw new RuntimeException(ex.getMessage(), ex);
		}
	}
https://www.ruoxue.org/?s=name+%25
s=name+%25
name %

decodeThrowException

URLDecoder Methods Java 建立一個 String ,使用 URLDecoder 指定不存在的編碼方案,進行解碼,會拋出例外。

	@Test
	public void decodeThrowException() {
		assertThatCode(() -> {
			String value = "email%40%21%24";
			System.out.println(value);
			String result = URLDecoder.decode(value, "X-8");
			System.out.println(result);
		}).isInstanceOf(UnsupportedEncodingException.class);
	}
email%40%21%24

decodeURL

URLDecoder Methods Java 建立一個 String ,使用 URLDecoder 指定編碼方案,如: UTF-8 ,進行解碼。

	@Test
	public void decodeURL() {
		try {
			String value = "https://www.ruoxue.org?amount=100&name=name+%25&user=user&email=email%40%21%24&timestamp=1470926696715";
			URI uri = new URI(value);
			String scheme = uri.getScheme();
			String host = uri.getHost();
			String query = uri.getRawQuery();
			System.out.println(query);
			String result = Arrays.stream(query.split("&")).map(e -> {
				try {
					String[] array = e.split("=");
					String param = URLDecoder.decode(array[0], StandardCharsets.UTF_8.toString()) + "="
							+ URLDecoder.decode(array[1], StandardCharsets.UTF_8.toString());
					return param;
				} catch (Exception ex) {
					throw new RuntimeException(ex.getMessage(), ex);
				}
			}).collect(Collectors.joining("&", scheme + "://" + host + "?", ""));
			System.out.println(result);
			assertThat(result).isEqualTo(
					"https://www.ruoxue.org?amount=101&name=name %&user=user&email=email@!$&timestamp=1470926696715");
		} catch (Exception ex) {
			throw new RuntimeException(ex.getMessage(), ex);
		}
	}
amount=101&name=name+%25&user=user&email=email%40%21%24&timestamp=1470926696715
https://www.ruoxue.org?amount=101&name=name %&user=user&email=email@!$&timestamp=1470926696715

URLDecoderMethodsTest.java

URLDecoder Methods in Java 新增單元測試,驗證 URLDecoder Functions in Java 是否符合預期。

package org.ruoxue.java_147.net;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatCode;

import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.stream.Collectors;

import org.junit.Test;

public class URLDecoderMethodsTest {

	@Test
	public void decode() {
		try {
			String value = "https%3A%2F%2Fwww.ruoxue.org";
			System.out.println(value);
			String result = URLDecoder.decode(value, StandardCharsets.UTF_8.toString());
			System.out.println(result);
			assertThat(result).isEqualTo("https://www.ruoxue.org");
		} catch (UnsupportedEncodingException ex) {
			throw new RuntimeException(ex.getMessage(), ex);
		}
	}

	@Test
	public void decodeQuery() {
		try {
			String value = "https://www.ruoxue.org/?s=name+%25";
			System.out.println(value);
			URI uri = new URI(value);
			String query = uri.getRawQuery();
			System.out.println(query);
			String[] array = query.split("=");
			String result = URLDecoder.decode(array[1], StandardCharsets.UTF_8.toString());
			System.out.println(result);
			assertThat(result).isEqualTo("name %");
		} catch (Exception ex) {
			throw new RuntimeException(ex.getMessage(), ex);
		}
	}

	@Test
	public void decodeThrowException() {
		assertThatCode(() -> {
			String value = "email%40%21%24";
			System.out.println(value);
			String result = URLDecoder.decode(value, "X-8");
			System.out.println(result);
		}).isInstanceOf(UnsupportedEncodingException.class);
	}

	@Test
	public void decodeURL() {
		try {
			String value = "https://www.ruoxue.org?amount=100&name=name+%25&user=user&email=email%40%21%24&timestamp=1470926696715";
			URI uri = new URI(value);
			String scheme = uri.getScheme();
			String host = uri.getHost();
			String query = uri.getRawQuery();
			System.out.println(query);
			String result = Arrays.stream(query.split("&")).map(e -> {
				try {
					String[] array = e.split("=");
					String param = URLDecoder.decode(array[0], StandardCharsets.UTF_8.toString()) + "="
							+ URLDecoder.decode(array[1], StandardCharsets.UTF_8.toString());
					return param;
				} catch (Exception ex) {
					throw new RuntimeException(ex.getMessage(), ex);
				}
			}).collect(Collectors.joining("&", scheme + "://" + host + "?", ""));
			System.out.println(result);
			assertThat(result).isEqualTo(
					"https://www.ruoxue.org?amount=101&name=name %&user=user&email=email@!$&timestamp=1470926696715");
		} catch (Exception ex) {
			throw new RuntimeException(ex.getMessage(), ex);
		}
	}
}

心得分享

URLDecoder Functions in Java 工具類別提供用於將 application/x-www-form-urlencoded MIME 格式轉換為 String 的靜態方法,對字串進行解碼,熟悉 URLDecoder Methods in Java 這些方法的操作,像是: decode 等方法。

發佈留言