HttpClient4.X发送带参数的Get请求「建议收藏」

HttpClient4.X发送带参数的Get请求「建议收藏」HttpClient是apache组织下面的一个用于处理HTTP请求和响应的开源工具。所用jar包为httpclient-4.3.6.jar、httpcore-4.3.3.jar、httpmime-4.3.6.jar、commons-codec-1.6.jar。发送Get请求代码如下:packagecom.zkn.newlearn.httpclient;importjava.

大家好,又见面了,我是你们的朋友全栈君。

HttpClient 是apache 组织下面的一个用于处理HTTP 请求和响应的开源工具。所用jar包为httpclient-4.3.6.jar、httpcore-4.3.3.jar、httpmime-4.3.6.jar、commons-codec-1.6.jar。

发送Get请求代码如下:

package com.zkn.newlearn.httpclient;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.List;

import org.apache.http.Consts;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.ParseException;
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.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import com.google.common.collect.Lists;

public class HttpClientTest02 {

	public static void main(String[] args) {
		CloseableHttpClient httpClient = HttpClients.createDefault();
		CloseableHttpResponse response = null;
		InputStream is = null;
		String url = "http://localhost:8080/MyWebxTest/getCityByProvinceEname.do";
		//封装请求参数
		List<NameValuePair> params = Lists.newArrayList();
		params.add(new BasicNameValuePair("cityEname", "henan"));
		String str = "";
		try {
			//转换为键值对
			str = EntityUtils.toString(new UrlEncodedFormEntity(params, Consts.UTF_8));
			System.out.println(str);
			//创建Get请求
			HttpGet httpGet = new HttpGet(url+"?"+str);
			//执行Get请求,
			response = httpClient.execute(httpGet);
			//得到响应体
			HttpEntity entity = response.getEntity();
			if(entity != null){
				is = entity.getContent();
				//转换为字节输入流
				BufferedReader br = new BufferedReader(new InputStreamReader(is, Consts.UTF_8));
				String body = null;
				while((body=br.readLine()) != null){
					System.out.println(body);
				}
			}
		} catch (ParseException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally{
			//关闭输入流,释放资源
			if(is != null){
				try {
					is.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			//消耗实体内容
			if(response != null){
				try {
					response.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			//关闭相应 丢弃http连接
			if(httpClient != null){
				try {
					httpClient.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		
	}
}

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/163276.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • charles打断点有什么用_charles打断点后 如何执行

    charles打断点有什么用_charles打断点后 如何执行前言Charles是收费软件,可以免费试用30天。试用期过后,未付费的用户仍然可以继续使用,但是每次使用时间不能超过30分钟,并且启动时将会有10秒种的延时。此时,我们只需网上找一个注册码即可解

    2022年7月29日
    12
  • 用递归方法求n的阶乘【C语言实现】

    用递归方法求n的阶乘【C语言实现】#include<stdio.h>intmain(){ longfac(intn); intn,y; printf(“inputanintegernumber:”); scanf(“%d”,&n); y=fac(n); printf(“%d!=%ld\n”,n,y); return0;}longfac(intn){ l…

    2022年5月4日
    85
  • 实验七 香农编码_香农编码效率可以大于1吗

    实验七 香农编码_香农编码效率可以大于1吗一、实验目的编程,对某一离散无记忆信源实现香农编码,输出消息符号及其对应的码字。设离散无记忆信源,。二进制香农编码过程如下:1、将信源发出的N个消息符号按其概率的递减次序依次排列。2、按下式计算第i个消息的二进制代码组的码长,并取整。3、为了编成唯一可译码,首先计算第i个消息的累加概率4、将累加概率Pi(为小数)变成二进制数5、除去小数点,并根据码长li,取小数点后li位数作为第i个消息的码字。二、实验环境Dev三、实验过程:#include<stdio.h>

    2025年10月18日
    4
  • loadrunner 压力测试工具下载

    loadrunner 压力测试工具下载loadrunner下载 http://willvvv.iteye.com/blog/1198158 用户名密码: user1  user1

    2022年7月17日
    15
  • linux部署tomcat项目详细教程(安装linux到部署tomcat)

    linux部署tomcat项目详细教程(安装linux到部署tomcat)近来想要研究下linux,所以就搭了个linux系统来配置服务器玩玩。这里分了个目录,如果已经安装好虚拟机或者linux系统的小伙伴可以直接跳过前面的安装介绍,直接看部署。文章目录一、总步骤说明二、安装虚拟机三、创建linux系统一、总步骤说明下载需要的材料(该博客有提供),这里我用到的主要有1)虚拟机Vmware,2)linux镜像文件CentOS-6.5-x86_64-bin-DVD1.iso3)服务器apache-tomcat-7.0.105.tar.gz4)jdk7u79linuxx

    2022年7月18日
    18

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号