Java Code Examples for org.apache.http.client.utils.URIBuilder#setParameters()

The following examples show how to use org.apache.http.client.utils.URIBuilder#setParameters() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HttpUtil.java    From java-pay with Apache License 2.0 6 votes vote down vote up
/**
 * 发送get请求
 *
 * @param url
 * @param params
 * @param header
 * @return
 */
public static String sendGet(String url, Map<String, Object> params, Map<String, Object> header) {
    String result = null;
    try {
        URIBuilder builder = new URIBuilder(url);
        if (params != null && params.size() > 0) {
            List<NameValuePair> pairs = new ArrayList<>();
            for (Map.Entry<String, Object> entry : params.entrySet()) {
                pairs.add(new BasicNameValuePair(entry.getKey(), entry.getValue().toString()));
            }
            builder.setParameters(pairs);
        }
        HttpGet httpGet = new HttpGet(builder.build());
        setHeaders(header, httpGet);
        HttpResponse response = httpClient.execute(httpGet);
        result = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return result;
}
 
Example 2
Source File: ArticleServiceImpl.java    From kaif with Apache License 2.0 6 votes vote down vote up
@VisibleForTesting
String canonicalizeUrl(String url) {
  //TODO visit target web page and get header:
  //   <link rel="canonical" href="https://blog.example.com/dresses/" />
  String cleaned = url.replaceAll("[\r\n \t]*", "");
  try {
    URI uri = new URI(cleaned);
    List<NameValuePair> params = URLEncodedUtils.parse(uri, Charsets.UTF_8);
    List<NameValuePair> cleanedParams = params.stream()
        .filter(pair -> !pair.getName().startsWith("utm_"))
        .sorted(Comparator.comparing(NameValuePair::getName)
            .thenComparing(NameValuePair::getValue))
        .collect(toList());
    URIBuilder uriBuilder = new URIBuilder(uri);
    if (cleanedParams.isEmpty()) {
      uriBuilder.clearParameters();
    } else {
      //set empty list will cause builder always append `?`
      uriBuilder.setParameters(cleanedParams);
    }
    return uriBuilder.build().toString();
  } catch (URISyntaxException e) {
    //ignore
  }
  return cleaned;
}
 
Example 3
Source File: AbstractHttpClient.java    From nano-framework with Apache License 2.0 6 votes vote down vote up
/**
 * 根据请求信息创建HttpRequestBase.
 *
 * @param cls     类型Class
 * @param url     URL
 * @param headers Http请求头信息列表
 * @param params  参数列表
 * @return HttpRequestBase
 */
protected HttpRequestBase createBase(final Class<? extends HttpRequestBase> cls, final String url, final Map<String, String> headers,
                                     final Map<String, String> params) {
    final URIBuilder builder = new URIBuilder();
    builder.setPath(url);

    final List<NameValuePair> pairs = covertParams2Nvps(params);
    builder.setParameters(pairs);

    try {
        final URI uri = builder.build();
        final HttpRequestBase base = ReflectUtils.newInstance(cls, uri);
        if (!CollectionUtils.isEmpty(headers)) {
            headers.forEach((key, value) -> base.addHeader(key, value));
        }

        return base;
    } catch (final Throwable e) {
        throw new HttpClientInvokeException(e.getMessage(), e);
    }
}
 
Example 4
Source File: HttpClientTools.java    From api-boot with Apache License 2.0 5 votes vote down vote up
public static String get(String url, Map<String, Object> params) throws URISyntaxException {
    URIBuilder ub = new URIBuilder();
    ub.setPath(url);

    ArrayList<NameValuePair> pairs = covertParams2NVPS(params);
    ub.setParameters(pairs);

    HttpGet httpGet = new HttpGet(ub.build());
    return getResult(httpGet);
}
 
Example 5
Source File: HttpClientTools.java    From api-boot with Apache License 2.0 5 votes vote down vote up
public static String get(String url, Map<String, Object> headers, Map<String, Object> params)
        throws URISyntaxException {
    URIBuilder ub = new URIBuilder();
    ub.setPath(url);

    ArrayList<NameValuePair> pairs = covertParams2NVPS(params);
    ub.setParameters(pairs);

    HttpGet httpGet = new HttpGet(ub.build());
    for (Map.Entry<String, Object> param : headers.entrySet()) {
        httpGet.addHeader(param.getKey(), String.valueOf(param.getValue()));
    }
    return getResult(httpGet);
}
 
Example 6
Source File: HttpClientUtil.java    From hdw-dubbo with Apache License 2.0 5 votes vote down vote up
public static String httpGetRequest(String url, Map<String, Object> params) throws URISyntaxException {
    URIBuilder ub = new URIBuilder();
    ub.setPath(url);

    ArrayList<NameValuePair> pairs = covertParams2NVPS(params);
    ub.setParameters(pairs);

    HttpGet httpGet = new HttpGet(ub.build());
    return getResult(httpGet);
}
 
Example 7
Source File: HttpClientUtil.java    From hdw-dubbo with Apache License 2.0 5 votes vote down vote up
public static String httpGetRequest(String url, Map<String, Object> headers,
                                    Map<String, Object> params) throws URISyntaxException {
    URIBuilder ub = new URIBuilder();
    ub.setPath(url);

    ArrayList<NameValuePair> pairs = covertParams2NVPS(params);
    ub.setParameters(pairs);

    HttpGet httpGet = new HttpGet(ub.build());
    for (Map.Entry<String, Object> param : headers.entrySet()) {
        httpGet.addHeader(param.getKey(), (String) param.getValue());
    }
    return getResult(httpGet);
}
 
Example 8
Source File: HttpRequestUtils.java    From seezoon-framework-all with Apache License 2.0 5 votes vote down vote up
public static String doGet(String url, Map<String, String> params) {
	Assert.hasLength(url, "请求地址为空");
	try {
		URIBuilder builder = new URIBuilder(url);
		builder.setParameters(getNameValuePair(params));
		HttpGet httpGet = new HttpGet(builder.toString());
		String result = execute(httpGet);
		return result;
	} catch (Exception e) {
		throw new ServiceException(e);
	}
}
 
Example 9
Source File: HttpClientUtils.java    From JobX with Apache License 2.0 5 votes vote down vote up
public static String httpGetRequest(String url, Map<String, Object> params) throws URISyntaxException {
    URIBuilder ub = new URIBuilder();
    ub.setPath(url);

    ArrayList<NameValuePair> pairs = covertParams2NVPS(params);
    ub.setParameters(pairs);

    HttpGet httpGet = new HttpGet(ub.build());
    return getResult(httpGet);
}
 
Example 10
Source File: HttpClientUtils.java    From JobX with Apache License 2.0 5 votes vote down vote up
public static String httpGetRequest(String url, Map<String, Object> headers, Map<String, Object> params)
        throws URISyntaxException {
    URIBuilder ub = new URIBuilder();
    ub.setPath(url);

    ArrayList<NameValuePair> pairs = covertParams2NVPS(params);
    ub.setParameters(pairs);

    HttpGet httpGet = new HttpGet(ub.build());
    for (Map.Entry<String, Object> param : headers.entrySet()) {
        httpGet.addHeader(param.getKey(), String.valueOf(param.getValue()));
    }
    return getResult(httpGet);
}
 
Example 11
Source File: AbstractHttpClient.java    From nano-framework with Apache License 2.0 5 votes vote down vote up
/**
 * 根据请求信息创建HttpRequestBase.
 *
 * @param cls    类型Class
 * @param url    URL
 * @param params 参数列表
 * @return HttpRequestBase
 */
protected HttpRequestBase createBase(final Class<? extends HttpRequestBase> cls, final String url, final Map<String, String> params) {
    final URIBuilder builder = new URIBuilder();
    builder.setPath(url);

    final List<NameValuePair> pairs = covertParams2Nvps(params);
    builder.setParameters(pairs);

    try {
        final URI uri = builder.build();
        return ReflectUtils.newInstance(cls, uri);
    } catch (final Throwable e) {
        throw new HttpClientInvokeException(e.getMessage(), e);
    }
}