Httpclient采用get方式

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;

import org.apache.http.NameValuePair;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
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.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("client")
public class HttpClient_Test {
@RequestMapping(value="test",produces="application/json;charset=utf-8")//设置编码格式
@ResponseBody
public String client(String msg){
	CloseableHttpClient createDefault = HttpClients.createDefault();//创建一个默认的HttpClients对象
	ArrayList<NameValuePair> arrayList = new ArrayList<NameValuePair>();//NameValuePair(类)
	arrayList.add(new BasicNameValuePair("city", msg));
	//arrayList.add(new BasicNameValuePair("key","free"));
	//arrayList.add(new BasicNameValuePair("appid","0"));//new BasiNameValuePair (BasicNameValuePair是存储键值对的类)
	
	String	paramsStr ="";
	try {
		UrlEncodedFormEntity urlEncodedFormEntity = new UrlEncodedFormEntity(arrayList,"UTF-8");//解决参数乱码问题
		paramsStr =EntityUtils.toString(urlEncodedFormEntity); //将字符串转化为json数据  需要关闭流
	} catch (ParseException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} catch (UnsupportedEncodingException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	
	HttpGet httpGet = new HttpGet("https://www.apiopen.top/weatherApi?city="+paramsStr); //创建一个get请求并发送参数
	
	httpGet.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1;"
			+ " Win64; x64; rv:50.0) Gecko/20100101 Firefox/50.0");
	httpGet.setHeader("Accept", "application/json");
	  
	httpGet.setHeader("Accept-Encoding", "gzip, deflate");
	httpGet.setHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");//设置http头部信息

	 RequestConfig config = RequestConfig.custom() //RequestConfig.Builder 配置器 ,我们可以通过custom获取一个新的Builder对象
				.setConnectTimeout(30000)  		//设置链接超时的时间1秒钟
				.setSocketTimeout(30000) 		//设置读取超时1秒钟
				.build(); 						//RequestConfig静态方法  setProxy  设置代理
	 httpGet.setConfig(config);//设置头部信息
	 
	 CloseableHttpResponse execute=null;//实例话对象赋值
	 String jsonStr="";
	 try {
		 execute = createDefault.execute(httpGet);//执行HttpClient
		 jsonStr=EntityUtils.toString(execute.getEntity(),"UTF-8");//转化json格式 并防止乱码
	} catch (ClientProtocolException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}finally{
		try {
			createDefault.close();//关闭HttpClient
			execute.close();//关闭执行
			httpGet.abort();//销毁GET请求
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	 
	return jsonStr;
}
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值