httpclient 访问网页面

来源:互联网 发布:mac电脑电池显示叉号 编辑:程序博客网 时间:2024/05/17 04:46

HttpClient 是 Apache Jakarta Common 下的子项目,可以用来提供高效的、最新的、功能丰富的支持 HTTP 协议的客户端编程工具包,并且它支持 HTTP 协议最新的版本和建议。


该功能支持以下情况:

1.http及https

2.get及post方式

3.ssl证书不验证

4.重定向

实现步骤如下:


1.pom文件中增加依赖包

<dependency>    <groupId>org.apache.httpcomponents</groupId>    <artifactId>httpclient</artifactId>    <version>4.5.1</version></dependency>
2.编写httpUtil类


import org.apache.http.HttpEntity;import org.apache.http.NameValuePair;import org.apache.http.client.ClientProtocolException;import org.apache.http.client.config.AuthSchemes;import org.apache.http.client.config.CookieSpecs;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.client.methods.HttpPost;import org.apache.http.config.Registry;import org.apache.http.config.RegistryBuilder;import org.apache.http.conn.socket.ConnectionSocketFactory;import org.apache.http.conn.socket.PlainConnectionSocketFactory;import org.apache.http.conn.ssl.NoopHostnameVerifier;import org.apache.http.conn.ssl.SSLConnectionSocketFactory;import org.apache.http.impl.client.CloseableHttpClient;import org.apache.http.impl.client.HttpClients;import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;import org.apache.http.message.BasicNameValuePair;import org.apache.http.util.EntityUtils;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import javax.net.ssl.SSLContext;import javax.net.ssl.TrustManager;import javax.net.ssl.X509TrustManager;import java.io.IOException;import java.io.UnsupportedEncodingException;import java.security.cert.CertificateException;import java.security.cert.X509Certificate;import java.util.ArrayList;import java.util.Arrays;import java.util.List;import java.util.Map;/** * Created by lip on 2016/4/19. */public class HttpUtil {    private final static Logger logger = LoggerFactory.getLogger(HttpUtil.class);    // 字符编码    public static String UTF8 = "UTF-8";    // 超时时间(毫秒)    public static Integer TIMEOUT = 30000;    public static Integer MAX_REDIRECTS = 10;    private static SSLConnectionSocketFactory socketFactory;    public static CloseableHttpResponse post(String url) {        return post(url,null,UTF8);    }    public static CloseableHttpResponse post(String url, Map<String, String> params) {        return post(url,params,UTF8);    }    public static CloseableHttpResponse post(String url, Map<String, String> params,String charset) {        // 创建默认的httpClient实例.        CloseableHttpClient httpclient = getHttpClient();        // 创建httppost        HttpPost httppost = new HttpPost(url);        // 创建参数队列        List<NameValuePair> formparams = new ArrayList<NameValuePair>();        if(params != null && params.size() > 0){            for (String key : params.keySet()){                formparams.add(new BasicNameValuePair(key, params.get(key)));            }        }        UrlEncodedFormEntity uefEntity;        StringBuffer logCnt = new StringBuffer();        CloseableHttpResponse response = null;        try {            uefEntity = new UrlEncodedFormEntity(formparams, charset);            httppost.setEntity(uefEntity);            logCnt.append("executing POST request " + httppost.getURI());            response = httpclient.execute(httppost);            try {                logCnt.append("\r\n");                logCnt.append(response.getStatusLine());                //跳转到重定向的url                if (response.getStatusLine().getStatusCode() == 302) {                    String locationUrl=response.getLastHeader("Location").getValue();                    logger.info(logCnt.toString());                    logCnt.delete(0,logCnt.length());                    get(locationUrl);                }else {                    HttpEntity entity = response.getEntity();                    logCnt.append("\r\n");                    logCnt.append("Response content: ");                    if (entity != null) {                        logCnt.append(EntityUtils.toString(entity, "UTF-8"));                    }                }            } finally {                logger.info(logCnt.toString());                response.close();                return response;            }        } catch (ClientProtocolException e) {            e.printStackTrace();        } catch (UnsupportedEncodingException e1) {            e1.printStackTrace();        } catch (IOException e) {            e.printStackTrace();        } finally {            // 关闭连接,释放资源            try {                httpclient.close();            } catch (IOException e) {                e.printStackTrace();            }            return response;        }    }    public static CloseableHttpResponse get(String url) {        return get(url,UTF8);    }    public static CloseableHttpResponse get(String url,String charset) {        // 创建默认的httpClient实例.        CloseableHttpClient httpclient = getHttpClient();        // 创建httpGet        HttpGet httpGet = new HttpGet(url);        StringBuffer logCnt = new StringBuffer();        CloseableHttpResponse response = null;        try {            logCnt.append("executing GET request " + httpGet.getURI());            response = httpclient.execute(httpGet);            try {                HttpEntity entity = response.getEntity();                logCnt.append("\r\n");                logCnt.append(response.getStatusLine());                logCnt.append("\r\n");                logCnt.append("Response content: ");                if (entity != null) {                    logCnt.append(EntityUtils.toString(entity, "UTF-8"));                }            } finally {                logger.info(logCnt.toString());                response.close();                return response;            }        } catch (ClientProtocolException e) {            e.printStackTrace();        } catch (UnsupportedEncodingException e1) {            e1.printStackTrace();        } catch (IOException e) {            e.printStackTrace();        } finally {            // 关闭连接,释放资源            try {                httpclient.close();            } catch (IOException e) {                e.printStackTrace();            }            return response;        }    }    private static CloseableHttpClient getHttpClient(){        enableSSL();        RequestConfig defaultRequestConfig = RequestConfig.custom()                .setCookieSpec(CookieSpecs.STANDARD_STRICT)                .setExpectContinueEnabled(true)                .setTargetPreferredAuthSchemes(Arrays.asList(AuthSchemes.NTLM, AuthSchemes.DIGEST))                .setProxyPreferredAuthSchemes(Arrays.asList(AuthSchemes.BASIC))                .setSocketTimeout(TIMEOUT)                .setConnectTimeout(TIMEOUT)                .setMaxRedirects(MAX_REDIRECTS)                .setRedirectsEnabled(true)                .build();        Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create()                .register("http", PlainConnectionSocketFactory.INSTANCE)                .register("https", socketFactory).build();        PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(socketFactoryRegistry);        CloseableHttpClient httpClient = HttpClients.custom()                .setConnectionManager(connectionManager)                .setDefaultRequestConfig(defaultRequestConfig)                .build();        return httpClient;    }    private  static void  enableSSL(){        try{            SSLContext context = SSLContext.getInstance("TLS");            context.init(null,new TrustManager[]{manager},null);            socketFactory = new SSLConnectionSocketFactory(context, NoopHostnameVerifier.INSTANCE);        }catch (Exception e){            e.printStackTrace();        }    }    private static TrustManager manager = new X509TrustManager() {        public void checkClientTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {        }        public void checkServerTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {        }        public X509Certificate[] getAcceptedIssuers() {            return null;        }    };}


0 0
原创粉丝点击