HttpClient4.X发送Get请求的参数拼接

使用httpClient发送get请求时,请求参数可以以?key=val&key1=val1的拼接到url后面。

但是请求参数较多时,这种方法比较麻烦,也不太优雅;研究了一下发现HttpClient4.X本身

是支持处理参数的。

1. 使用 URIBuilder来构建请求URI

httpclient相关的jar包mvn依赖:

<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
<version>4.4.3</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.1</version>
</dependency>
import com.google.common.collect.Lists;
import org.apache.http.Consts;
import org.apache.http.HttpEntity;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils; private HttpGet buildHttpGet(String url, Map<String, String> para)
throws URISyntaxException {
URIBuilder builder = new URIBuilder(url);
Set<String> set = para.keySet();
for(String key: set){
builder.setParameter(key, para.get(key));
}
HttpGet request = new HttpGet(builder.build());
RequestConfig requestConfig = RequestConfig.custom()
.setSocketTimeout(6000)
.setConnectTimeout(6000)
.setConnectionRequestTimeout(6000).build();
request.setConfig(requestConfig);
System.out.println(request.getURI().toString());
return request;
}

2. 使用 NameValuePair 来拼接URI

List<NameValuePair> params = Lists.newArrayList();
params.add(new BasicNameValuePair("cityEname", "henan"));
String str = "";
//转换为键值对
str = EntityUtils.toString(new UrlEncodedFormEntity(params, Consts.UTF_8));
System.out.println(str);
HttpGet httpGet = new HttpGet(url + "?" + str);

3. 根据 HttpGet反向获取键值对列表

HttpGet request = new HttpGet("http://example.com/?var=1&var=2");
URIBuilder newBuilder = new URIBuilder(request.getURI());
//获取键值对列表
List<NameValuePair> params = newBuilder.getQueryParams();
//转换为键值对字符串
String str = EntityUtils.toString(new UrlEncodedFormEntity(params, Consts.UTF_8));

HttpClient4.X发送Get请求的url参数拼接的更多相关文章

  1. Jquery发送ajax请求以及datatype参数为text/JSON方式

    Jquery发送ajax请求以及datatype参数为text/JSON方式 1.方式一:datatype:'text' 2.方式二:datatype:'JSON' 3.使用gson-1.5.jar包 ...

  2. 为什么返回的数据前面有callback? ashx/json.ashx?的后面加 callback=? 起什么作用 js url?callback=xxx xxx的介绍 ajax 跨域请求时url参数添加callback=?会实现跨域问题

    为什么返回的数据前面有callback?   这是一个同学出现的问题,问到了我. 应该是这样的: 但问题是这样的: 我看了所请求的格式和后台要求的也是相同的.而且我也是这种做法,为什么他的就不行呢? ...

  3. feignclient发送get请求,传递参数为对象

    feignclient发送get请求,传递参数为对象.此时不能使用在地址栏传递参数的方式,需要将参数放到请求体中. 第一步: 修改application.yml中配置feign发送请求使用apache ...

  4. C#发送Post请求,带参数,不带参数,指定参数

    1.不带参数发送Post请求 /// <summary> /// 指定Post地址使用Get 方式获取全部字符串 /// </summary> /// <param na ...

  5. C# 发送Post请求(带参数)

    此处内容传输都是用UTF-8编码 1.不带参数发送Post请求 /// <summary> /// 指定Post地址使用Get 方式获取全部字符串 /// </summary> ...

  6. ajax 跨域请求时url参数添加callback=?会实现跨域问题

    例如: 1.在 jQuery 中,可以通过使用JSONP 形式的回调函数来加载其他网域的JSON数据,如 "myurl?callback=?".jQuery 将自动替换 ? 为正确 ...

  7. 使用restClient工具发送post请求并带参数

    运行 restClient 点 Method选项卡,选中post方法 然后切换到 Body选项卡,点右边的 倒三角,选 String body 出现如下窗口: 点击右边红圈里的按钮,弹出窗口: 点是, ...

  8. 使用restTemplate发送post请求,传入参数是在requestBody请求体中,以json形式传输

    @PostMapping public ResponseResult add(User user){ HttpHeaders httpHeaders = new HttpHeaders(); Medi ...

  9. POST 发送HTTP请求入参为:String url, Map<String, Object> propsMap

    /** * 发送HTTP请求 * * @param url * @param propsMap * 发送的参数 */ public static String httpSend(String url, ...

随机推荐

  1. 常用的SQL语句(牢记)

    上课时的重要内容,其中表 t_hq, t_hq2, 以及字段的名字是举例说明. update t_hq t set t.bumendh = '10086';commit;全表更新电话,commit是提 ...

  2. hdoj1087 (DP--LIS)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1087 思路:这题很简单了,纯LIS的解法,没有一点变形,由于数据小,使用O(n^2)LIS解法就足够了 ...

  3. tf.random_normal()

    tf.random_normal()函数用于从服从指定正太分布的数值中取出指定个数的值. tf.random_normal(shape, mean=0.0, stddev=1.0, dtype=tf. ...

  4. 123. Best Time to Buy and Sell Stock III (Array; DP)

    Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...

  5. 全屏API

    by zhangxinxu from http://www.zhangxinxu.com本文地址:http://www.zhangxinxu.com/wordpress/?p=2679 二.相关文章以 ...

  6. C语言 链表基本函数

    #include <stdio.h> #include <malloc.h> typedef struct my_node mynode; struct my_node{    ...

  7. for() 和$.each()的用法区别

    一.对于数组 var arr=['姚明','易建联','张继科']; $.each(arr,function(index,value){ document.write(index+"=&qu ...

  8. 2018.07.20 bzoj1614: Telephone Lines架设电话线(二分+最短路)

    传送门 这题直接做显然gg" role="presentation" style="position: relative;">gggg,看这数据 ...

  9. HDU 3177 Crixalis's Equipment (贪心,差值)

    题意:判断 n 件物品是否可以搬进洞里,每件物品有实际体积A和移动时的额外体积 B . 析:第一反应就是贪心,一想是不是按B从大到小,然后一想,不对,比如体积是20,第一个 是A=11, B=19.第 ...

  10. HDU 1756 Cupid's Arrow (几何问题,判定点在多边形内部)

    题意:中文的么,自己看喽. 析:很容易明白是判定点是不是在多边形内部,一般是向量来判定,我一开始用点在向量的右侧,因为是顺时针给的,只要点全在外侧或边上, 就可以,暴力一下就ok.由于这个是浮点数,一 ...