如何在java中解析此URL

时间:2014-04-21 06:23:31

标签: java apache url httpclient urlencode

我有这样的网址

http://my.my.info/action/doning/something?mailParams=iCgGugAIdMW3CqkYbZ/dGYVqljerVjzbKLvTQCyuosHzxisIrgYf8rcKqRhtn90Z0eVGZ+vx43P4g+umFmddNdDufWv/nDwbCgqBwHs9OYVd5g4VKuFO4jTfF1NiW+KjUi3JubtJT+0F7p+wPHEpTRwJJ+O0eevojx6DioK3cLGejz5UdfIrqzOVNT05TaPKFie4yZxbXfA=

我在键值对中需要它而不是查询字符串。

我的出局应该是

mailParams = iCgGugAIdMW3CqkYbZ/dGYVqljerVjzbKLvTQCyuosHzxisIrgYf8rcKqRhtn90Z0eVGZ+vx43P4g+umFmddNdDufWv/nDwbCgqBwHs9OYVd5g4VKuFO4jTfF1NiW+KjUi3JubtJT+0F7p+wPHEpTRwJJ+O0eevojx6DioK3cLGejz5UdfIrqzOVNT05TaPKFie4yZxbXfA=

由于以上编码,我无法解析此字符串。我使用URLEncodedUtils来解析它,但它返回

mailParams = iCgGugAIdMW3CqkYbZ/dGYVqljerVjzbKLvTQCyuosHzxisIrgYf8rcKqRhtn90Z0eVGZ vx43P4g umFmddNdDufWv/nDwbCgqBwHs9OYVd5g4VKuFO4jTfF1NiW KjUi3JubtJT 0F7p wPHEpTRwJJ O0eevojx6DioK3cLGejz5UdfIrqzOVNT05TaPKFie4yZxbXfA 

哪个不相关可以帮助我这样做吗?

3 个答案:

答案 0 :(得分:2)

尝试使用java.net.URL

以下示例代码。

    URL aURL = new URL("http://my.my.info/action/doning/something?mailParams=iCgGugAIdMW3CqkYbZ/dGYVqljerVjzbKLvTQCyuosHzxisIrgYf8rcKqRhtn90Z0eVGZ+vx43P4g+umFmddNdDufWv/nDwbCgqBwHs9OYVd5g4VKuFO4jTfF1NiW+KjUi3JubtJT+0F7p+wPHEpTRwJJ+O0eevojx6DioK3cLGejz5UdfIrqzOVNT05TaPKFie4yZxbXfA=");

    System.out.println("protocol = " + aURL.getProtocol());
    System.out.println("authority = " + aURL.getAuthority());
    System.out.println("host = " + aURL.getHost());
    System.out.println("port = " + aURL.getPort());
    System.out.println("path = " + aURL.getPath());
    System.out.println("query = " + aURL.getQuery());
    System.out.println("filename = " + aURL.getFile());
    System.out.println("ref = " + aURL.getRef());

答案 1 :(得分:1)

如果需要拆分链接以获取参数

使用String#split将正则表达式作为参数

String link="http://my.my.info/action/doning/something?mailParams=iCgGugAIdMW3CqkYbZ/dGYVqljerVjzbKLvTQCyuosHzxisIrgYf8rcKqRhtn90Z0eVGZ+vx43P4g+umFmddNdDufWv/nDwbCgqBwHs9OYVd5g4VKuFO4jTfF1NiW+KjUi3JubtJT+0F7p+wPHEpTRwJJ+O0eevojx6DioK3cLGejz5UdfIrqzOVNT05TaPKFie4yZxbXfA=";
        String[] mailparams=link.split("\\?");
        System.out.print(mailparams[1]);

你也可以 使用URL#getQuery

String link="http://my.my.info/action/doning/something?mailParams=iCgGugAIdMW3CqkYbZ/dGYVqljerVjzbKLvTQCyuosHzxisIrgYf8rcKqRhtn90Z0eVGZ+vx43P4g+umFmddNdDufWv/nDwbCgqBwHs9OYVd5g4VKuFO4jTfF1NiW+KjUi3JubtJT+0F7p+wPHEpTRwJJ+O0eevojx6DioK3cLGejz5UdfIrqzOVNT05TaPKFie4yZxbXfA=";
         URL aURL = new URL(link);
        System.out.println( aURL.getQuery());

<强>输出

mailParams=iCgGugAIdMW3CqkYbZ/dGYVqljerVjzbKLvTQCyuosHzxisIrgYf8rcKqRhtn90Z0eVGZ+vx43P4g+umFmddNdDufWv/nDwbCgqBwHs9OYVd5g4VKuFO4jTfF1NiW+KjUi3JubtJT+0F7p+wPHEpTRwJJ+O0eevojx6DioK3cLGejz5UdfIrqzOVNT05TaPKFie4yZxbXfA=

答案 2 :(得分:0)

URL aURL = null;
try {
    aURL = new URL("http://my.my.info/action/doning/something?mailParams=iCgGugAIdMW3CqkYbZ/dGYVqljerVjzbKLvTQCyuosHzxisIrgYf8rcKqRhtn90Z0eVGZ+vx43P4g+umFmddNdDufWv/nDwbCgqBwHs9OYVd5g4VKuFO4jTfF1NiW+KjUi3JubtJT+0F7p+wPHEpTRwJJ+O0eevojx6DioK3cLGejz5UdfIrqzOVNT05TaPKFie4yZxbXfA=");
} catch (MalformedURLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

System.out.println(aURL.getQuery());