Java uses HTTPS post to send JSON data

Source: Internet
Author: User

package com.saygou.weixin;import java.io.bytearrayoutputstream;import java.io.dataoutputstream; import java.io.ioexception;import java.io.inputstream;import java.net.url;import  java.security.keymanagementexception;import java.security.nosuchalgorithmexception;import  java.security.cert.certificateexception;import java.security.cert.x509certificate;import  javax.net.ssl.hostnameverifier;import javax.net.ssl.httpsurlconnection;import  Javax.net.ssl.sslcontext;import javax.net.ssl.sslsession;import javax.net.ssl.trustmanager;import  javax.net.ssl.x509trustmanager;public class httpsutil {private static class  TrustAnyTrustManager implements X509TrustManager {public void  Checkclienttrusted (X509certificate[] chain, string authtype) throws certificateexception  {}public void checkservertrusted (X509certificate[] chain, strinG authtype) Throws certificateexception {}public x509certificate[] getacceptedissuers ( )  {return new x509certificate[] {};}} private static class trustanyhostnameverifier implements hostnameverifier { Public boolean verify (string hostname, sslsession session)  {return true;}} /** * post Mode Request Server (HTTPS protocol)  *  *  @param  url *              Request Address  *  @param  content *              Parameters  *  @param  charset *              Coding  *  @return  *  @throws   nosuchalgorithmexception *  @throws  KeyManagementException *  @throws  ioexception  */public static byte[] post (string url, string&Nbsp;content, string charset) Throws nosuchalgorithmexception, keymanagementexception, Ioexception {sslcontext sc = sslcontext.getinstance ("SSL"); Sc.init (null, new  Trustmanager[] { new trustanytrustmanager ()  },new java.security.securerandom ()); Url console = new url (URL); httpsurlconnection conn =  (httpsurlconnection)  console.openconnection (); Conn.setsslsocketfactory (Sc.getsocketfactory ()); Conn.sethostnameverifier (New trustanyhostnameverifier ()); Conn.setdooutput (True); Conn.connect ();D Ataoutputstream out = new dataoutputstream ( Conn.getoutputstream ()); Out.write (Content.getbytes (charset));//  refresh, close Out.flush (); Out.close (); InputStream  is = conn.getinputstream ();if  (is != null)  {ByteArrayOutputStream  Outstream = new bytearrayoutputstream (); byte[] buffer = new byte[1024];int  len = 0;while  (len = is.read (buffer))  != -1)  {outstream.write (buffer,  0, len);} Is.close (); Return outstream.tobytearray ();} Return null;}}


Java uses HTTPS post to send JSON data

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.