- 1 public void Connect(HttpGet httpGet) {
- 2 String str = "";//返回结果
- 3 CloseableHttpResponse response = null;
- 4 try {
- 5 //1、创建httpClient
- 6 CloseableHttpClient client = null;
- 7 try {
- 8 //把代理设置到请求配置 代理IP 端口
- 9 HttpHost proxy = new HttpHost(PROXY_URL, PROXY_PROT);
- 10 //超时时间单位为毫秒
- 11 RequestConfig defaultRequestConfig = RequestConfig.custom()
- 12 .setConnectTimeout(CONNECTION_TIME_OUT).setSocketTimeout(CONNECTION_TIME_OUT)
- 13 .setProxy(proxy).build();
- 14 client = HttpClients.custom().setDefaultRequestConfig(defaultRequestConfig).build();
- 15
- 16 response = client.execute(httpGet);
- 17 } catch (UnknownHostException hostEx) {
- 18 hostEx.printStackTrace();
- 19 }
- 20 //2、获取实体
- 21 HttpEntity entity = response.getEntity();
- 22 //将实体装成字符串
- 23 str = EntityUtils.toString(entity);
- 24 System.out.println("返回结果---" + str);
- 25 response.close();
- 26 } catch (Exception e) {
- 27 e.printStackTrace();
- 28 }
- 29 30 }
最初没有负责的都是外网项目,没有经验。查看之后原来这么简单!