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;
}
}