创建Java URL时需要转义'%'

时间:2018-09-18 14:28:16

标签: java url character-encoding uri special-characters

我无法创建有效的URL,因为它需要包含一些特殊字符。

String address = "...";
URL url = new URL(address);
URI uri = new URI(url.getProtocol(), url.getUserInfo(), url.getHost(), url.getPort(), url.getPath(), url.getQuery(), url.getRef());
url = uri.toURL();
InputStream input = url.openStream();
....

问题是我的地址需要包含&作为字符串中的字符之一,表示为%26。当我将地址创建为:

String address = "http://foo.com/?animal=monkey&banana=fruit%20%26%20delicious";

...变成

"http://foo.com/?animal=monkey&banana=fruit%20%2526%20delicious"

在URL到URI的返回过程中。

解决此问题的最佳方法是什么?我在URL / URI转换中使用最佳做法吗?如果是这样,是否有办法在26之前转义'%',使其最后仍为%26?

MCVE:

import java.util.*;
import java.io.*;
import java.net.*;

public class Test {
    public static void main(String[] args) {
        try {
            String address = "http://foo.com/?animal=monkey&banana=fruit%20%26%20delicious";
            URL url = new URL(address);
            URI uri = new URI(url.getProtocol(), url.getUserInfo(), url.getHost(), url.getPort(), url.getPath(), url.getQuery(), url.getRef());
            url = uri.toURL();
            System.out.println("url: " + url);
        } catch (Exception e) {
            System.out.println("Bad!");
        }
    }

}

终端I / O:

$ javac Test.java
$ java Test
url: http://foo.com/?animal=monkey&banana=fruit%2520%2526%2520delicious

换句话说,如何获取此URL:

url: http://foo.com/?animal=monkey&banana=fruit%20%26%20delicious

1 个答案:

答案 0 :(得分:0)

进一步研究之后,我无法使用此方法创建URL。我需要使用仅包含单个String的URI构造函数。

我使用以下代码来嗅出差异并突出问题所在:

import java.util.*;
import java.io.*;
import java.net.*;

public class Test {
    public static void main(String[] args) {
        ArrayList<String> addresses = new ArrayList<String>();
        addresses.add("http://foo.com/?animal=monkey&banana=fruit%20%26%20delicious");
        addresses.add("http://foo.com/?animal=monkey&banana=fruit %26 delicious");
        addresses.add("http://foo.com/?animal=monkey&banana=fruit & delicious");
        //addresses.add("http://foo.com/?animal=monkey&banana=fruit \%26 delicious"); this uses an illegal escape character

        for (String address : addresses) {
            System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
            try {
                URL url = new URL(address);
                URI uri = new URI(url.getProtocol(), url.getUserInfo(), url.getHost(), url.getPort(), url.getPath(), url.getQuery(), url.getRef());
                URL urlA = uri.toURL();
                System.out.println("address: " + address);
                System.out.println("yields: ");
                System.out.println("  * urlA: " + urlA);
                uri = new URI(address);
                URL urlB = uri.toURL();
                System.out.println("  * urlB: " + urlB);
            } catch (Exception e) {
                System.out.println("  Problem!");
            }
        }
    }

}

哪个是在终端中生成的输出:

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
address: http://foo.com/?animal=monkey&banana=fruit%20%26%20delicious
yields: 
  * urlA: http://foo.com/?animal=monkey&banana=fruit%2520%2526%2520delicious
  * urlB: http://foo.com/?animal=monkey&banana=fruit%20%26%20delicious
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
address: http://foo.com/?animal=monkey&banana=fruit %26 delicious
yields: 
  * urlA: http://foo.com/?animal=monkey&banana=fruit%20%2526%20delicious
  Problem!
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
address: http://foo.com/?animal=monkey&banana=fruit & delicious
yields: 
  * urlA: http://foo.com/?animal=monkey&banana=fruit%20&%20delicious
  Problem!

唯一的urlB结果在第一个试用版中有效。