zoukankan      html  css  js  c++  java
  • https authorization basic

    /*
     * To change this license header, choose License Headers in Project Properties.
     * To change this template file, choose Tools | Templates
     * and open the template in the editor.
     */
    package com.ndkey.auditproxy.cityon;
    
    import com.fasterxml.jackson.databind.ObjectMapper;
    import java.io.IOException;
    import java.security.cert.CertificateException;
    import java.security.cert.X509Certificate;
    import java.util.HashMap;
    import javax.net.ssl.SSLContext;
    import javax.net.ssl.TrustManager;
    import javax.net.ssl.X509TrustManager;
    import org.apache.http.auth.AuthScope;
    import org.apache.http.auth.UsernamePasswordCredentials;
    import org.apache.http.client.CredentialsProvider;
    import org.apache.http.client.methods.CloseableHttpResponse;
    import org.apache.http.client.methods.HttpPost;
    import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
    import org.apache.http.entity.ContentType;
    import org.apache.http.entity.StringEntity;
    import org.apache.http.impl.client.BasicCredentialsProvider;
    import org.apache.http.impl.client.CloseableHttpClient;
    import org.apache.http.impl.client.HttpClients;
    import org.apache.http.util.EntityUtils;
    
    /**
     *
     * @author zxf
     */
    public class TestMain {
    
        private static CloseableHttpClient httpClient = null;
        private final static ObjectMapper _objectMapper = new ObjectMapper();
    
        public static void main(String[] args) throws IOException, Exception {
            System.setProperty("jsse.enableSNIExtension", "false");
            SSLContext sslContext = SSLContext.getInstance("TLSv1.2");//TLS
            sslContext.init(null, trustAllCerts, new java.security.SecureRandom());
            SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext);
            CredentialsProvider credsProvider = new BasicCredentialsProvider();
            credsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials("wifi", "12345678"));
            httpClient = HttpClients.custom().setSSLSocketFactory(sslsf).setDefaultCredentialsProvider(credsProvider).build();
            //      httpClient = HttpClients.custom().setSSLSocketFactory(sslsf).build();
            CloseableHttpResponse response = null;
            try {
                HashMap<String, String> map = new HashMap<String, String>();
                map.put("name", "123");
                map.put("mobile", "18600000006");
                HttpPost httpPost = new HttpPost("https://uat15.acxiom.com.cn/cityon/resources/customer");
                String info = _objectMapper.writeValueAsString(map);
                //  httpPost.addHeader("Content-Type", "application/json");
                //  httpPost.addHeader("Authorization", "Basic " + Base64.encode("wifi:12345678".getBytes()));
            //    httpPost.setConfig(RequestConfig.custom().setConnectTimeout(5 * 1000).setConnectionRequestTimeout(5 * 10000).build());
                httpPost.setEntity(new StringEntity(info, ContentType.APPLICATION_JSON));
                response = httpClient.execute(httpPost);
                String res = EntityUtils.toString(response.getEntity());
    
                System.out.println("回复消息:");
                System.out.println(res);
            } catch (Exception ex) {
                throw new Exception(ex);
            } finally {
                if (response != null) {
                    response.close();
                }
            }
    
            try {
                httpClient.close();
            } catch (IOException ex) {
    
            }
        }
        private static final TrustManager[] trustAllCerts = new TrustManager[]{new X509TrustManager() {
            public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                return new java.security.cert.X509Certificate[]{};
            }
    
            public void checkClientTrusted(X509Certificate[] chain,
                    String authType) throws CertificateException {
            }
    
            public void checkServerTrusted(X509Certificate[] chain,
                    String authType) throws CertificateException {
            }
        }};
    }
  • 相关阅读:
    内网/局域网 DNS获取不正确
    Qt5.7 + D2D渲染引擎。 画面闪烁,几乎没图像显示
    安网路由器 移动宽带和电信宽带混用问题解决
    安网路由器 静态IP和PPOE混用时,如果设置了路由器定时重启可能导致路由器罢工
    Qt5.7+VS2015环境下使用QtCreator编译QtAV视频库
    Android Gradle编译so库或运行时出现 text relocations 崩溃的正确解决方法
    使用Jenkins + shell(gradle)快速搭建 Android 构建机
    Android 获得控件在屏幕中的坐标
    Mac 共享WiFi给任意设备(Android,Iphone等等)
    股票、外汇、期货、数字货币小总结
  • 原文地址:https://www.cnblogs.com/littlehoom/p/5364740.html
Copyright © 2011-2022 走看看