HttpClient v4.3 例子: Https, Basic Auth, Get/Post

Https 例子:

HttpHost host = new HttpHost(ip, 443, "https");

public CloseableHttpClient createHttpsClient() {
    X509TrustManager x509mgr = new X509TrustManager() {
        @Override
        public void checkClientTrusted(X509Certificate[] xcs, String string) {
        }
        @Override
        public void checkServerTrusted(X509Certificate[] xcs, String string) {
        }
        @Override
        public X509Certificate[] getAcceptedIssuers() {
            return null;
        }
    };

    SSLContext sslContext = SSLContext.getInstance("TLS");
    sslContext.init(null, new TrustManager[] { x509mgr }, null);
    SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext, SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);

    return HttpClients.custom().setSSLSocketFactory(sslsf).build();
}
Basic Auth:
HttpHost host = new HttpHost(ip, 443, "https");

private HttpClientContext createBasicAuthContext(String username, String password) {
    CredentialsProvider credsProvider = new BasicCredentialsProvider();
    Credentials defaultCreds = new UsernamePasswordCredentials(username, password);
    credsProvider.setCredentials(new AuthScope(host.getHostName(), host.getPort()), defaultCreds);

    AuthCache authCache = new BasicAuthCache();
    BasicScheme basicAuth = new BasicScheme();
    authCache.put(host, basicAuth);

    HttpClientContext context = HttpClientContext.create();
    context.setCredentialsProvider(credsProvider);
    context.setAuthCache(authCache);
    return context;
}
doGet:
private CloseableHttpResponse doGet(String url, Map<String, String> headers) throws IOException {
    HttpGet get = new HttpGet(url);
    for (Map.Entry<String, String> header : headers.entrySet()) {
        get.setHeader(header.getKey(), header.getValue());
    }
    HttpContext context = createBasicAuthContext("admin", "password");
    CloseableHttpResponse response = client.execute(host, get, context);
    try {
        // status = response.getStatusLine().getStatusCode();
        // headers = response.getAllHeaders();

        // HttpEntity entity = response.getEntity();
        // text = IOUtils.toString(entity.getContent(), "ISO-8859-1");

        return response;
    } finally {
        response.close();
    }
}
doPost:
private void doPost(String url, Map<String, String> headers, String body) throws IOException {
    HttpPost post = new HttpPost(url);
    for (Map.Entry<String, String> header : headers.entrySet()) {
        post.setHeader(header.getKey(), header.getValue());
    }
    post.setEntity(new StringEntity(body));
    HttpContext context = createBasicAuthContext("admin", "password");
    CloseableHttpResponse response = client.execute(host, post, context);
    try {
        // status = response.getStatusLine().getStatusCode();
        // headers = response.getAllHeaders();

        // HttpEntity entity = response.getEntity();
        // text = IOUtils.toString(entity.getContent(), "ISO-8859-1");
    } finally {
        response.close();
    }
}

你可能感兴趣的:(httpclient)