JAVA uses httpclient for HTTPS interface calls __java

Source: Internet
Author: User

1. To avoid the need for a certificate, use a class to inherit the Defaulthttpclient class, ignoring the checksum process.

Import java.security.cert.CertificateException;
Import Java.security.cert.X509Certificate;
Import Javax.net.ssl.SSLContext;
Import Javax.net.ssl.TrustManager;
Import Javax.net.ssl.X509TrustManager;
Import Org.apache.http.conn.ClientConnectionManager;
Import Org.apache.http.conn.scheme.Scheme;
Import Org.apache.http.conn.scheme.SchemeRegistry;
Import Org.apache.http.conn.ssl.SSLSocketFactory;

Import org.apache.http.impl.client.DefaultHttpClient; /** * httpclient * @ClassName for HTTPS requests: Sslclient * @Description: TODO * @author Devin <xxx> * @date 2017
        February 7 pm 1:42:07 * */public class Sslclient extends Defaulthttpclient {public sslclient () throws
        Super ();
        Sslcontext CTX = sslcontext.getinstance ("TLS"); X509trustmanager TM = new X509trustmanager () {@Override public void checkclienttrusted (X5
             09certificate[] chain, String authtype) throws Certificateexception {   @Override public void checkservertrusted (x509certificate[] chain, String authtype) throws certificateexception {} @Override public X50
                9certificate[] Getacceptedissuers () {return null;
        }
        };
        Ctx.init (NULL, new TRUSTMANAGER[]{TM}, NULL);
        Sslsocketfactory SSF = new Sslsocketfactory (ctx,sslsocketfactory.allow_all_hostname_verifier);
        Clientconnectionmanager CCM = This.getconnectionmanager ();
        schemeregistry sr = Ccm.getschemeregistry ();
    Sr.register (New Scheme ("https", 443, SSF)); }
}

2. Create a tool class that uses httpclient to send post requests

Import org.apache.http.HttpEntity;
Import Org.apache.http.HttpResponse;
Import Org.apache.http.StatusLine;
Import org.apache.http.client.HttpClient;
Import Org.apache.http.client.methods.HttpPost;
Import org.apache.http.entity.StringEntity;
Import Org.apache.http.message.BasicHeader;
Import Org.apache.http.util.EntityUtils; /** * @ClassName: Httpclientutil * @Description: TODO * @author Devin <xxx> * @dat for post requests using httpclient E February 7, 2017 pm 1:43:38 * */public class Httpclientutil {@SuppressWarnings ("resource") public static String D
        Opost (String url,string jsonstr,string charset) {httpclient = null;
        HttpPost httppost = null;
        String result = null;
            try{httpclient = new Sslclient ();
            HttpPost = new HttpPost (URL);
            Httppost.addheader ("Content-type", "Application/json");
            Stringentity se = new stringentity (JSONSTR);
            Se.setcontenttype ("Text/json"); SE.setcontentencoding (New Basicheader ("Content-type", "Application/json"));
            Httppost.setentity (SE);
            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 ();
    return result; }
}

3. Test code

public static void Main (string[] args) { 
        String url = ' https://192.168.1.101/xxx ';
        String jsonstr = "{xxx}";
        String httporgcreatetestrtn = httpclientutil.dopost (URL, jsonstr, "Utf-8");
    }


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.