是否有 Java 包来处理构建 URL?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1861620/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 18:17:29  来源:igfitidea点击:

Is there a Java package to handle building URLs?

javaurl

提问by Bialecki

What I'm looking for specifically is some code in Java that will take a Mapobject and convert it into a query string that I can append to a URL I return. I'm sure there's a library that does this and much more, but I can't find it with a quick Google search. Anyone know of one that will do this?

我要特别寻找的是 Java 中的一些代码,它将获取一个Map对象并将其转换为查询字符串,我可以将其附加到我返回的 URL 中。我确信有一个图书馆可以做到这一点以及更多,但我无法通过快速的谷歌搜索找到它。有谁知道一个可以做到这一点?

采纳答案by miku

I found apache httpcomponentsto be a solid and versatile library for dealing with HTTP in Java. However, here's a sample class, which might suffice for building URL query strings:

我发现apache httpcomponents是一个可靠且通用的库,用于在 Java 中处理 HTTP。但是,这里有一个示例类,它可能足以构建 URL 查询字符串:

import java.net.URLEncoder;

public class QueryString {

    private String query = "";

    public QueryString(HashMap<String, String> map) {
        Iterator it = mp.entrySet().iterator();
        while (it.hasNext()) {
            Map.Entry pairs = (Map.Entry)it.next();
            query += URLEncoder.encode(pairs.getKey(), "utf-8") + "=" +
            URLEncoder.encode(pairs.getValue(), "utf-8");
            if (it.hasNext()) { query += "&"; }
        }
    }

    public QueryString(Object name, Object value) {
        query = URLEncoder.encode(name.toString(), "utf-8") + "=" +
            URLEncoder.encode(value.toString(), "utf-8");
    }

    public QueryString() { query = ""; }

    public synchronized void add(Object name, Object value) {
        if (!query.trim().equals("")) query += "&";
        query += URLEncoder.encode(name.toString(), "utf-8") + "=" +
            URLEncoder.encode(value.toString(), "utf-8");
    }

    public String toString() { return query; }
}

Usage:

用法:

HashMap<String, String> map = new HashMap<String, String>();
map.put("hello", "world");
map.put("lang", "en");

QueryString q = new QueryString(map);
System.out.println(q);
// => "hello=world&lang=en"

回答by Thraidh

Try URIBuilderfrom Apache Http Compoments(HttpClient 4).

Apache Http 组件(HttpClient 4)尝试URIBuilder

It does not actually take a map, but is well suited for building URIs.

它实际上并不需要地图,但非常适合构建 URI。

回答by Reverend Gonzo

There's thisonline, so you can simply call any of:

这个在线,所以你可以简单地调用任何一个:

InputStream serverInput = post(URL url, Map parameters); 
InputStream serverInput = post(URL url, Map parameters); 
InputStream serverInput = post(URL url, Map cookies, Map parameters); 
InputStream serverInput = post(URL url, String[] cookies, Object[] parameters); 
InputStream serverInput = post(URL url, Object[] parameters).

He provides the source code too.

他也提供了源代码。