org.apache.httpcomponents.httpclient

2021-04-26 05:28

阅读:666

标签:cer   object   pos   methods   pst   aml   osi   list   tor   

apache org doc :http://hc.apache.org/httpcomponents-client-ga/tutorial/html/fundamentals.html#d5e49

依赖:


        log4j
            log4j
            1.2.17org.apache.httpcomponents
    httpmime
    4.5org.apache.httpcomponents
    httpclient
    4.5org.apache.httpcomponents
    httpcore
    4.4.1commons-codec
    commons-codec
    1.3commons-logging
    commons-logging
    1.1.1

  

import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import javax.net.ssl.SSLContext;
import org.apache.http.conn.ssl.NoopHostnameVerifier;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.ssl.SSLContexts;
import org.apache.http.ssl.TrustStrategy;
import org.apache.log4j.Logger;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.util.EntityUtils;
import org.apache.http.HttpStatus;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


public class HttpDemo {
    private static Logger LOGGER = Logger.getLogger(HttpDemo.class);
    static CloseableHttpResponse response;
    static HttpEntity entity;

    /**
     * this is ssl ignore https CloseableClient
     */
    public static CloseableHttpClient sslIgnoreClient() throws KeyStoreException, NoSuchAlgorithmException, KeyManagementException {
        CloseableHttpClient client = null;
        SSLContext sslContext = SSLContexts.custom().loadTrustMaterial(null, new TrustStrategy() {

            public boolean isTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {
                return true;
            }
        }).build();
        client = HttpClients.custom().setSSLContext(sslContext).setSSLHostnameVerifier(new NoopHostnameVerifier()).build();

        return client;
    }

    /**
     * http get method
     */
    static String doGet(String url, Map header) throws KeyStoreException, IOException,
            KeyManagementException, NoSuchAlgorithmException {
        String result = null;  // initialize result
        CloseableHttpClient request = sslIgnoreClient();
        HttpGet msg = new HttpGet(url);
        if (header != null) {
            header.forEach((key,value)-> {msg.addHeader(key.toString(),value.toString()); });

        }
        response = request.execute(msg);
        if(response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
            entity = response.getEntity();
            result = EntityUtils.toString(entity,"UTF-8");
        }
        return result;
    }
    /**  String body is json type*/
    static String doPost(String url,String body, Map header)throws KeyStoreException, IOException,
    KeyManagementException, NoSuchAlgorithmException{
        String result =null;
        CloseableHttpClient request = sslIgnoreClient();
        HttpPost msg = new HttpPost(url);
        if (header !=null){
            header.forEach((key,value)-> {msg.addHeader(key.toString(),value.toString()); });
        }
        msg.setEntity(new StringEntity(body));
        response = request.execute(msg);
        if(response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
            entity = response.getEntity();
            result = EntityUtils.toString(entity,"UTF-8");
        }
        return result;
    }

    /**  do post form-data  */
    static String doPostParam(String url, Map header,Map params)throws KeyStoreException, IOException,
            KeyManagementException, NoSuchAlgorithmException{
        String result = null;
        CloseableHttpClient request =sslIgnoreClient();
        HttpPost msg = new HttpPost();
        if (header !=null){
            header.forEach((key,value)-> {msg.addHeader(key.toString(),value.toString()); });
        }
        List paramList=new ArrayList();
        params.forEach((key,value)->{paramList.add(new BasicNameValuePair(key.toString(),value.toString()));});
        HttpEntity formEntity = new UrlEncodedFormEntity(paramList,"utf-8");
        msg.setEntity(formEntity);
        response = request.execute(msg);
        if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK){
            entity = response.getEntity();
            result = EntityUtils.toString(entity,"UTF-8");
        }
        return result;
    }

    /**  do  upload and download operation*/
    static  void uploadFile(){

    }

}

  

org.apache.httpcomponents.httpclient

标签:cer   object   pos   methods   pst   aml   osi   list   tor   

原文地址:https://www.cnblogs.com/SunshineKimi/p/12222791.html


评论


亲,登录后才可以留言!