Java Send HTTPS request

Source: Internet
Author: User
Tags getmessage readline static class
Package Com.yichangmao;
Import Java.io.BufferedReader;
Import java.io.IOException;
Import Java.io.InputStreamReader;
Import Java.io.PrintWriter;
Import java.io.UnsupportedEncodingException;
Import java.net.HttpURLConnection;
Import java.net.MalformedURLException;
Import Java.net.URL;
Import Java.net.URLEncoder;
Import java.security.cert.CertificateException;
Import Java.security.cert.X509Certificate;
Import Java.util.HashMap;

Import Java.util.Iterator;


Import javax.net.ssl.*; public class Trustssl {private static class Trustanytrustmanager implements X509trustmanager {public Vo ID checkclienttrusted (x509certificate[] chain, String authtype) throws certificateexception {} publi c void Checkservertrusted (x509certificate[] chain, String authtype) throws certificateexception {} p
        Ublic x509certificate[] Getacceptedissuers () {return new x509certificate[]{}; }} private Static Class TrustanyhostNameverifier implements Hostnameverifier {public boolean verify (String hostname, sslsession session) {
        return true; }/** * http request * @param URL * @param data * @return * @throws ioexception/public static St
		Ring requesturl (String URL, hashmap<string, string> data) throws IOException {HttpURLConnection conn;
			try {//if get ...//url requesturl = new URL (url + "?" + httpbuildquery (data));
			URL requesturl = new URL (URL);
		conn = (httpurlconnection) requesturl.openconnection ();
		catch (Malformedurlexception e) {return e.getmessage ();

		} conn.setrequestproperty ("Content-type", "application/x-www-form-urlencoded");
		Conn.setdoinput (TRUE);

		Conn.setdooutput (TRUE);
		PrintWriter writer = new PrintWriter (Conn.getoutputstream ());
		Writer.print (Httpbuildquery (data));
		Writer.flush ();

		Writer.close ();
		String Line;
		BufferedReader BufferedReader;
		StringBuilder sb = new StringBuilder (); InpUtstreamreader streamReader = null;
		try {streamReader = new InputStreamReader (Conn.getinputstream (), "UTF-8");
			The catch (IOException e) {/* Boolean Ret2 = true;
			if (Ret2) {return e.getmessage ();
		} * * StreamReader = new InputStreamReader (Conn.geterrorstream (), "UTF-8");
				finally {if (StreamReader!= null) {BufferedReader = new BufferedReader (StreamReader);
				SB = new StringBuilder ();
				while (line = Bufferedreader.readline ())!= null) {sb.append (line);
	}} return sb.tostring (); /** * Parameter code * @param data * @return/public static String Httpbuildquery (hashmap<string, string> dat
		A) {String ret = "";
		String K, V;
		Iterator<string> iterator = Data.keyset (). iterator ();
			while (Iterator.hasnext ()) {k = Iterator.next ();
			v = data.get (k);
			try {ret = Urlencoder.encode (k, "UTF8") + "=" + Urlencoder.encode (V, "UTF8"); catch (Unsupportedencodingexception e) {} ret = "& ";}
	Return ret.substring (0, Ret.length ()-1); public static string Senghttpsget (string url,hashmap<string, string> data) throws Exception {SS
        Lcontext 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.connect ();

		Conn.setrequestproperty ("Content-type", "application/x-www-form-urlencoded");
		Conn.setdoinput (TRUE);

		Conn.setdooutput (TRUE);
		PrintWriter writer = new PrintWriter (Conn.getoutputstream ());
		Writer.print (Httpbuildquery (data));
		Writer.flush ();

		Writer.close ();
		String Line;
		BufferedReader BufferedReader;
		StringBuilder sb = new StringBuilder (); InputStreamReader StreamReader = null;
		try {streamReader = new InputStreamReader (Conn.getinputstream (), "UTF-8");
			The catch (IOException e) {/* Boolean Ret2 = true;
			if (Ret2) {return e.getmessage ();
		} * * StreamReader = new InputStreamReader (Conn.geterrorstream (), "UTF-8");
				finally {if (StreamReader!= null) {BufferedReader = new BufferedReader (StreamReader);
				SB = new StringBuilder ();
				while (line = Bufferedreader.readline ())!= null) {sb.append (line);
    }} return sb.tostring (); }
}

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.