HTTPS Authorization Basic

Source: Internet
Author: User

/** To change the license header, choose License Headers in the Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ PackageCom.ndkey.auditproxy.cityon;ImportCom.fasterxml.jackson.databind.ObjectMapper;Importjava.io.IOException;Importjava.security.cert.CertificateException;Importjava.security.cert.X509Certificate;ImportJava.util.HashMap;ImportJavax.net.ssl.SSLContext;ImportJavax.net.ssl.TrustManager;ImportJavax.net.ssl.X509TrustManager;ImportOrg.apache.http.auth.AuthScope;Importorg.apache.http.auth.UsernamePasswordCredentials;ImportOrg.apache.http.client.CredentialsProvider;ImportOrg.apache.http.client.methods.CloseableHttpResponse;ImportOrg.apache.http.client.methods.HttpPost;Importorg.apache.http.conn.ssl.SSLConnectionSocketFactory;ImportOrg.apache.http.entity.ContentType;Importorg.apache.http.entity.StringEntity;ImportOrg.apache.http.impl.client.BasicCredentialsProvider;Importorg.apache.http.impl.client.CloseableHttpClient;Importorg.apache.http.impl.client.HttpClients;Importorg.apache.http.util.EntityUtils;/** * * @authorZXF*/ Public classTestmain {Private StaticCloseablehttpclient httpClient =NULL; Private Final StaticObjectmapper _objectmapper =NewObjectmapper ();  Public Static voidMain (string[] args)throwsIOException, Exception {system.setproperty ("Jsse.enablesniextension", "false"); Sslcontext Sslcontext= Sslcontext.getinstance ("TLSv1.2");//TLSSslcontext.init (NULL, Trustallcerts,Newjava.security.SecureRandom ()); Sslconnectionsocketfactory SSLSF=Newsslconnectionsocketfactory (Sslcontext); Credentialsprovider Credsprovider=NewBasiccredentialsprovider (); Credsprovider.setcredentials (Authscope.any,NewUsernamepasswordcredentials ("WiFi", "12345678")); HttpClient=Httpclients.custom (). Setsslsocketfactory (SSLSF). Setdefaultcredentialsprovider (Credsprovider). build (); //httpClient = Httpclients.custom (). Setsslsocketfactory (SSLSF). build ();Closeablehttpresponse response =NULL; Try{HashMap<string, string> map =NewHashmap<string, string>(); Map.put ("Name", "123"); Map.put ("Mobile", "18600000006"); HttpPost HttpPost=NewHttpPost ("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 *). Setconnectionrequesttimeout (5 * 10000). Build ());Httppost.setentity (Newstringentity (info, contenttype.application_json)); Response=Httpclient.execute (HttpPost); String Res=entityutils.tostring (Response.getentity ()); System.out.println ("Reply message:");        System.out.println (RES); } Catch(Exception ex) {Throw NewException (ex); } finally {            if(Response! =NULL) {response.close (); }        }        Try{httpclient.close (); } Catch(IOException ex) {}}Private Static Finaltrustmanager[] Trustallcerts =Newtrustmanager[]{NewX509trustmanager () { Publicjava.security.cert.x509certificate[] Getacceptedissuers () {return Newjava.security.cert.x509certificate[]{}; }         Public voidcheckclienttrusted (x509certificate[] chain, String authtype)throwscertificateexception {} Public voidcheckservertrusted (x509certificate[] chain, String authtype)throwscertificateexception {}}};}

HTTPS Authorization Basic

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.