短信验证服务---HttpClient

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_38375620/article/details/79557655
package com.issCollege.banbiShop.potal.controller;

import org.apache.http.HttpEntity;
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.util.EntityUtils;
import org.apache.log4j.Logger;


public class HttpClientController {
	/*private Logger log=Logger.getLogger(this.getClass().getName());
	@Test
	public void test()	throws Exception{
	//1. 创建HttpClient连接对象。
	CloseableHttpClient client = HttpClients.createDefault();
	//2. 创建请求方法的实例,并指定请求URL。
	HttpGet get = new HttpGet("http://localhost:9080/banbiShop/rest/goods/list.action?cid=8");
	//4. 调用HttpClient对象的execute(HttpUriRequest request)发送请求,该方法返回一个HttpResponse。
	CloseableHttpResponse response = client.execute(get);
	//3. 如果需要发送请求参数,可调用HttpGet、HttpPost共同的setParams(HetpParams params)方法来添加请求参数;
	//对于HttpPost对象而言,也可调用setEntity(HttpEntity entity)方法来设置请求参数。
	//获得响应状态吗
	int code = response.getStatusLine().getStatusCode();
	log.debug("状态"+code);
	//5. 调用HttpResponse的getAllHeaders()、getHeaders(String name)等方法可获取服务器的响应头;
	//调用HttpResponse的getEntity()方法可获取HttpEntity对象,该对象包装了服务器的响应内容。程序可通过该对象获取服务器的响应内容。
	HttpEntity entity = response.getEntity();
	String string = EntityUtils.toString(entity, "UTF-8");
	//6. 释放连接。
	System.out.println("内容"+string);
	}
	
	@Test
	public void test () throws Exception {
		CloseableHttpClient client = HttpClients.createDefault();
		HttpGet get = new  HttpGet("http://localhost:9080/banbiShop/rest/goods/list2/8");
		CloseableHttpResponse response = client.execute(get);
		int code = response.getStatusLine().getStatusCode();
		System.out.println("状态"+code);
		HttpEntity entity = response.getEntity();	
		String string = EntityUtils.toString(entity,"UTF-8");
		System.out.println("内容"+string);
		client.close();
	
	}
	*/

}

猜你喜欢

转载自blog.csdn.net/qq_38375620/article/details/79557655