HttpClient中GET和POST请求方式详解

前述,个人小结

使用HttpClient发送请求、接收响应很简单,一般需要如下几步即可。

1. 创建HttpClient对象。

 2. 创建请求方法的实例,并指定请求URL。如果需要发送GET请求,创建HttpGet对象;如果需要发送POST请求,创建HttpPost对象。

3. 如果需要发送请求参数,可调用HttpGet、HttpPost共同的setParams(HttpParams params)方法来添加请求参数;对于HttpPost对象而言,也可调用setEntity(HttpEntity entity)方法来设置请求参数。

 4. 调用HttpClient对象的execute(HttpUriRequest request)发送请求,该方法返回一个HttpResponse。

5. 调用HttpResponse的getAllHeaders()、getHeaders(String name)等方法可获取服务器的响应头;调用HttpResponse的getEntity()方法可获取HttpEntity对象,该对象包装了服务器的响应内容。程序可通过该对象获取服务器的响应内容。

 6. 释放连接。无论执行方法是否成功,都必须释放连接

第一、导入HttpCLient的pom依赖

  <dependencies>
	  	<dependency>
			<groupId>org.apache.httpcomponents</groupId>
			<artifactId>httpclient</artifactId>
			<version>4.5.3</version>
		</dependency>
		<dependency>
			<groupId>org.apache.httpcomponents</groupId>
			<artifactId>httpcore</artifactId>
			<version>4.4.7</version>
		</dependency>
  </dependencies>

注意:版本号可选

第二、代码实现---httpClient发送get请求(带参数直接在ulr后跟参数拼接)

	public void HttpClientGet() throws Exception {
		// 获取http客户端
		CloseableHttpClient client = HttpClients.createDefault();
		// 通过httpget方式来实现我们的get请求
		HttpGet httpGet = new HttpGet("http://www.dve10086.cn?username=zhangsan&password=lisi");
		// 通过client调用execute方法,得到我们的执行结果就是一个response,所有的数据都封装在response里面了
		CloseableHttpResponse Response = client.execute(httpGet);
      if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
		// HttpEntity是一个中间的桥梁,在httpClient里面,是连接我们的请求与响应的一个中间桥梁,所有的请求参数都是通过HttpEntity携带过去的
		// 所有的响应的数据,也全部都是封装在HttpEntity里面
		HttpEntity entity = Response.getEntity();
		// 通过EntityUtils 来将我们的数据转换成字符串
		String str = EntityUtils.toString(entity, "UTF-8");
		System.out.println(str);
      }
		// 关闭
		Response.close();
	}

httpClient发送POST请求(带参数请求可看参考如下代码)

	public void HttpClientPost() throws Exception {
		// 获取默认的请求客户端
		CloseableHttpClient client = HttpClients.createDefault();
		// 通过HttpPost来发送post请求
		HttpPost httpPost = new HttpPost("http://www.dev10086.cn");
		/*
		 * post带参数开始
		 */
		// 第一步:构造list集合,添加数据
		List<NameValuePair> list = new ArrayList<NameValuePair>();
		BasicNameValuePair basicNameValuePair = new BasicNameValuePair("username", "zhangsan");
		BasicNameValuePair basicNameValuePair2 = new BasicNameValuePair("password", "123456");
		list.add(basicNameValuePair);
		list.add(basicNameValuePair2);
		// 第二步:我们发现Entity是一个接口,所以只能找实现类,发现实现类又需要一个集合,集合的泛型是NameValuePair类型
		UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(list);
		// 第三步:通过setEntity 将我们的entity对象传递过去
		httpPost.setEntity(formEntity);
		/*
		 * post带参数结束
		 */
		// HttpEntity是一个中间的桥梁,在httpClient里面,是连接我们的请求与响应的一个中间桥梁,所有的请求参数都是通过HttpEntity携带过去的
		// 通过client来执行请求,获取一个响应结果
		CloseableHttpResponse response = client.execute(httpPost);
       if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
		    HttpEntity entity = response.getEntity();
		    String content = EntityUtils.toString(entity, "UTF-8");
            JSONObject jsonObject = JSON.parseObject(content);
		    System.out.println(str); //输出数据,这里可以写IO了
        }
		// 关闭
		response.close();
	}

猜你喜欢

转载自blog.csdn.net/SHYLOGO/article/details/105261449