Java中的HTTP URL地址编码 [英] HTTP URL Address Encoding in Java

查看:634
本文介绍了Java中的HTTP URL地址编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Java独立应用程序从用户获取一个URL(指向文件),我需要点击并下载它。我面临的问题是我无法正确编码HTTP URL地址...

My Java standalone application gets a URL (which points to a file) from the user and I need to hit it and download it. The problem I am facing is that I am not able to encode the HTTP URL address properly...

示例:

URL:  http://search.barnesandnoble.com/booksearch/first book.pdf

java.net.URLEncoder.encode(url.toString(), "ISO-8859-1");

给我回报:

http%3A%2F%2Fsearch.barnesandnoble.com%2Fbooksearch%2Ffirst+book.pdf

但是,我想要的是

http://search.barnesandnoble.com/booksearch/first%20book.pdf

(空格由%20代替)

我猜 URLEncoder 不是为编码HTTP URL而设计的... JavaDoc说用于HTML表单编码的实用程序类......有没有其他方法可以做到这一点?

I guess URLEncoder is not designed to encode HTTP URLs... The JavaDoc says "Utility class for HTML form encoding"... Is there any other way to do this?

推荐答案

java.net.URI 类可以提供帮助;在您找到的URL文档中

The java.net.URI class can help; in the documentation of URL you find


注意,URI类在某些情况下会执行其组件字段的转义。管理URL编码和解码的推荐方法是使用URI

Note, the URI class does perform escaping of its component fields in certain circumstances. The recommended way to manage the encoding and decoding of URLs is to use an URI

使用一个带有多个参数的构造函数,喜欢:

Use one of the constructors with more than one argument, like:

URI uri = new URI(
    "http", 
    "search.barnesandnoble.com", 
    "/booksearch/first book.pdf",
    null);
URL url = uri.toURL();
//or String request = uri.toString();

(URI的单参数构造函数不会转义非法字符)

只有非法字符会被上面的代码转义 - 它不会转义非ASCII字符(请参阅fatih的评论)。 br>
toASCIIString 方法可用于获取仅包含US-ASCII字符的String:

Only illegal characters get escaped by above code - it does NOT escape non-ASCII characters (see fatih's comment).
The toASCIIString method can be used to get a String only with US-ASCII characters:

URI uri = new URI(
    "http", 
    "search.barnesandnoble.com", 
    "/booksearch/é",
    null);
String request = uri.toASCIIString();






对于带有<$ c的查询的网址$ c>http://www.google.com/ig/api?weather=SãoPaulo,使用构造函数的5参数版本:


For an URL with a query like http://www.google.com/ig/api?weather=São Paulo, use the 5-parameter version of the constructor:

URI uri = new URI(
        "http", 
        "www.google.com", 
        "/ig/api",
        "weather=São Paulo",
        null);
String request = uri.toASCIIString();

这篇关于Java中的HTTP URL地址编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆