package com.spf; import org.apache.http.HttpEntity; import org.apache.http.NameValuePair; import org.apache.http.client.HttpClient; 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.HttpPost; 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 java.io.IOException; import java.util.*; /** * Created by SPF */ public class HttpClientUtils { public static String post(String url, Map<String, Object> map) { CloseableHttpClient client =null; HttpPost post = null; CloseableHttpResponse response = null; String result = null; try { client = HttpClients.createDefault(); post = new HttpPost(url); //设置超时时间 RequestConfig requestConfig = RequestConfig.custom() .setConnectTimeout(2000) //设置链接超时时间 .setSocketTimeout(8000)//设置读取超时时间 .build(); post.setConfig(requestConfig); //设置请求头信息,模拟浏览器请求 post.setHeader("User-Agent","Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.86 Safari/537.36"); //设置参数 if(map != null && !map.isEmpty()) { List<NameValuePair> list = new ArrayList<NameValuePair>(); Iterator iterator = map.entrySet().iterator(); while(iterator.hasNext()){ Map.Entry<String,Object> elem = (Map.Entry<String, Object>) iterator.next(); list.add(new BasicNameValuePair(elem.getKey(),elem.getValue().toString())); } if(list.size() > 0){ UrlEncodedFormEntity entity = new UrlEncodedFormEntity(list,"UTF-8"); post.setEntity(entity); } } //执行请求 response = client.execute(post); if(response != null){ HttpEntity resEntity = response.getEntity(); if(resEntity != null){ result = EntityUtils.toString(resEntity,"UTF-8"); } } } catch (Exception e) { e.printStackTrace(); } finally { try { client.close(); response.close(); } catch (IOException e) { e.printStackTrace(); } } return result; }
public static String get(String url, Map<String, String> map) { CloseableHttpClient httpClient = HttpClients.createDefault(); CloseableHttpResponse response = null; InputStream is = null; //封装请求参数 List<NameValuePair> params = Lists.newArrayList(); Iterator iterator = map.entrySet().iterator(); while(iterator.hasNext()){ Map.Entry<String,Object> elem = (Map.Entry<String, Object>) iterator.next(); if (elem.getValue() == null) { continue; } params.add(new BasicNameValuePair(elem.getKey(),elem.getValue().toString())); } String str = ""; String body = null; String str2 = ""; 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(); str2 = EntityUtils.toString(entity,"UTF-8"); // if(entity != null){ // is = entity.getContent(); // //转换为字节输入流 // BufferedReader br = new BufferedReader(new InputStreamReader(is, Consts.UTF_8)); // while((body=br.readLine()) != null){ // System.out.println(body); // } // } } catch (Exception 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(); } } } return str2; }
}
附:设置代理IP
//设置代理IP HttpHost httpHost = new HttpHost("115.197.169.12",808); RequestConfig requestConfig = RequestConfig.custom().setProxy(httpHost).build(); post.setConfig(requestConfig);