How to request HTTPS using Java

Source: Internet
Author: User

TLS version for JDK (for reference only)

1. Write a sslclient class, inherit to HttpClient

Importjava.security.cert.CertificateException; Importjava.security.cert.X509Certificate; ImportJavax.net.ssl.SSLContext; ImportJavax.net.ssl.TrustManager; ImportJavax.net.ssl.X509TrustManager; ImportOrg.apache.http.conn.ClientConnectionManager; ImportOrg.apache.http.conn.scheme.Scheme; ImportOrg.apache.http.conn.scheme.SchemeRegistry; Importorg.apache.http.conn.ssl.SSLSocketFactory; Importorg.apache.http.impl.client.DefaultHttpClient; //HttpClient for HTTPS requests Public classSslclientextendsdefaulthttpclient{ PublicSslclient ()throwsexception{Super();
     //transport protocol needs to be judged by its own Sslcontext CTX= Sslcontext.getinstance ("TLSv1.2"); X509trustmanager TM=NewX509trustmanager () {@Override Public voidcheckclienttrusted (x509certificate[] chain, String authtype)throwscertificateexception {} @Override Public voidcheckservertrusted (x509certificate[] chain, String authtype)throwscertificateexception {} @Override Publicx509certificate[] Getacceptedissuers () {return NULL; } }; Ctx.init (NULL,NewTRUSTMANAGER[]{TM},NULL); Sslsocketfactory SSF=Newsslsocketfactory (Ctx,sslsocketfactory.allow_all_hostname_verifier); Clientconnectionmanager CCM= This. Getconnectionmanager (); Schemeregistry SR=Ccm.getschemeregistry (); Sr.register (NewScheme ("https", 443, SSF)); } }

2. Write a class that uses httpclient to send a POST request

Importjava.util.ArrayList; ImportJava.util.Iterator; Importjava.util.List; ImportJava.util.Map; ImportJava.util.Map.Entry; Importorg.apache.http.HttpEntity; ImportOrg.apache.http.HttpResponse; ImportOrg.apache.http.NameValuePair; Importorg.apache.http.client.HttpClient; Importorg.apache.http.client.entity.UrlEncodedFormEntity; ImportOrg.apache.http.client.methods.HttpPost; ImportOrg.apache.http.message.BasicNameValuePair; Importorg.apache.http.util.EntityUtils; /** Tool class for Post requests with HttpClient*/   Public classHttpclientutil { PublicString DoPost (String url,map<string,string>map,string CharSet) {HttpClient HttpClient=NULL; HttpPost HttpPost=NULL; String result=NULL; Try{httpClient=Newsslclient (); HttpPost=Newhttppost (URL); //Setting Parameterslist<namevaluepair> list =NewArraylist<namevaluepair>(); Iterator Iterator=Map.entryset (). iterator ();  while(Iterator.hasnext ()) {Entry<String,String> Elem = (entry<string, string>) Iterator.next (); List.add (NewBasicnamevaluepair (Elem.getkey (), Elem.getvalue ())); }              if(List.size () > 0) {urlencodedformentity entity=Newurlencodedformentity (List,charset);              Httppost.setentity (entity); } HttpResponse Response=Httpclient.execute (HttpPost); if(Response! =NULL) {httpentity resentity=response.getentity (); if(Resentity! =NULL) {result=entityutils.tostring (Resentity,charset); }              }          }Catch(Exception ex) {ex.printstacktrace (); }          returnresult; }  }  

3. Calling the requested test code

ImportJava.util.HashMap; ImportJava.util.Map; //Testing the interface Public classTestmain {PrivateString url = "https://192.168.1.101/"; PrivateString charset = "Utf-8"; PrivateHttpclientutil Httpclientutil =NULL;  PublicTestmain () {Httpclientutil=NewHttpclientutil (); }             Public voidTest () {String httporgcreatetest= URL + "Httporg/create"; Map<String,String> Createmap =NewHashmap<string,string>(); Createmap.put ("Authuser", "* * * * *"); Createmap.put ("Authpass", "* * * * *"); Createmap.put ("Orgkey", "* * *"); Createmap.put ("OrgName", "* * *"); String Httporgcreatetestrtn=Httpclientutil.dopost (Httporgcreatetest,createmap,charset); System.out.println ("Result:" +Httporgcreatetestrtn); }             Public Static voidMain (string[] args) {Testmain main=NewTestmain ();      Main.test (); }  }  

    Rack Package used : Download

How to request HTTPS using Java

Related Article

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.