HTTPClient 4 - 使用参数生成 url

HTTPClient 4 - generate url with params

如何获得此请求的完整 URL?我只需要 http://whatever?client_id=xyz...。我如何构建一个 URL?

None of print语句打印我想要的,getURI()不加参数...

    HttpGet get = new HttpGet(authUrlTemplate);
    BasicHttpParams bhp = new BasicHttpParams();

    bhp.setParameter("client_id", getClientId());
    bhp.setParameter("client_secret", getClientSecret());
    bhp.setParameter("redirect_uri", redirectURL.toString());
    bhp.setParameter("grant_type", "authorization_code");

    get.setParams(bhp);
    System.err.println(get.getURI());
    System.err.println(get.getRequestLine());
    System.err.println(get.getParams());

输出:

21:18:49,967 ERROR [stderr] (default task-7) https://accounts.google.com/o/oauth2/v2/auth
21:18:49,970 ERROR [stderr] (default task-7) GET https://accounts.google.com/o/oauth2/v2/auth HTTP/1.1
21:18:49,971 ERROR [stderr] (default task-7) org.apache.http.params.BasicHttpParams@28310ff2

我知道有一个库可以做到这一点,但我想这样做。

这是一个通用的工作示例。代码很冗长,可以缩短,但这给了你一个整体的想法。

import org.apache.http.NameValuePair;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.BasicHttpParams;
import sun.management.resources.agent;

import java.util.*;

/**
 * Created by aditya on 9/24/2016.
 */
public class SOMain {
    static public void main(String[] args){
        String baseUrl="http://google.co.in";
        HashMap<String,String> parameters = new HashMap<String,String>();
        parameters.put("key1","value1");
        parameters.put("key3","value3");
        parameters.put("key2","value2");
        System.out.println(addParameters(baseUrl,parameters));
    }

    static protected String addParameters(String url, HashMap<String,String> parameters){
        if(!url.endsWith("?"))
            url += "?";

        List<NameValuePair> params = new LinkedList<NameValuePair>();
        if(parameters!=null){
            Iterator entries = parameters.entrySet().iterator();

            while (entries.hasNext()) {
                Map.Entry entry = (Map.Entry) entries.next();
                String key = (String)entry.getKey();
                String value = (String)entry.getValue();
                params.add(new BasicNameValuePair(key, value));            }
        }

        String paramString = URLEncodedUtils.format(params, "utf-8");

        url += paramString;
        return url;
    }
}

这会打印

http://google.co.in?key1=value1&key2=value2&key3=value3

下面是我如何操作的简单版本:

public String getAuthUrl() {
    URIBuilder b = null;
    // Removed exception handling...
    b = new URIBuilder(AUTH_URL); // http://whatever.com

    List<NameValuePair> nvps = new ArrayList<NameValuePair>();
    nvps.add(new BasicNameValuePair("client_id", getClientId()));
    nvps.add(new BasicNameValuePair("redirect_uri", redirectURL.toString()));
    nvps.add(new BasicNameValuePair("state", STATE));
    nvps.add(new BasicNameValuePair("response_type", "code"));
    nvps.add(new BasicNameValuePair("scope", "email"));
    b.setParameters(nvps);
    return b.toString();
  }