HTTPS sends a POST request [pro-Test available]

Source: Internet
Author: User

1, directly built a tool class can be put
/*** Send HTTPS request to shared body*/ Public  StaticJsonobject sendpost (String url,string parame,map<string,object> pmap)throwsIOException, Keymanagementexception, nosuchalgorithmexception, nosuchproviderexception{//Request ResultJsonobject JSON =NewJsonobject (); PrintWriter out=NULL; BufferedReader in=NULL; String result= "";    URL Realurl;    Httpsurlconnection Conn; String Method= "POST"; //Enquiry AddressString queryString =URL; //Request Parameter AcquisitionString Postpar = ""; //String Request Parameters    if(parame!=NULL) {Postpar=Parame; }    //request parameters in map format    if(pmap!=NULL) {StringBuffer MSTR=NewStringBuffer ();  for(String Str:pmap.keySet ()) {string Val=(String) pmap.get (str); Try{val=urlencoder.encode (Val, "UTF-8"); } Catch(unsupportedencodingexception e) {e.printstacktrace (); } mstr.append (str+ "=" +val+ "&"); }        //Final ParametersPostpar =mstr.tostring (); intLasts=postpar.lastindexof ("&"); Postpar=postpar.substring (0, lasts); }    if(Method.touppercase (). Equals ("GET") ) {queryString+="?" +Postpar; } sslsocketfactory SSF=httpsclientutils.getssfactory (); Try{Realurl=NewURL (queryString); Conn=(httpsurlconnection) realurl.openconnection ();        Conn.setsslsocketfactory (SSF); Conn.setrequestproperty ("Content-type", "application/x-www-form-urlencoded"); Conn.setrequestproperty ("Accept", "*/*"); Conn.setrequestproperty ("Connection", "keep-alive"); Conn.setrequestproperty ("User-agent",                "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1) "); if(Method.touppercase (). Equals ("POST") {conn.setdooutput (true); Conn.setdoinput (true); Conn.setusecaches (false); out=NewPrintWriter (Conn.getoutputstream ());            Out.print (Postpar);        Out.flush (); }Else{conn.connect (); } in=NewBufferedReader (NewInputStreamReader (Conn.getinputstream (), "Utf-8"));        String Line;  while(line = In.readline ())! =NULL) {result+=Line ; } JSON=jsonobject.fromobject (Result); }finally {        Try {            if(Out! =NULL) {out.close (); }            if(In! =NULL) {in.close (); }        } Catch(IOException ex) {ex.printstacktrace (); }    }    returnJSON;}
2. Packages that may be required
ImportJava.io.BufferedReader;Importjava.io.IOException;ImportJava.io.InputStreamReader;ImportJava.io.PrintWriter;Importjava.io.UnsupportedEncodingException;ImportJava.net.URI;ImportJava.net.URL;ImportJava.net.URLEncoder;Importjava.security.KeyManagementException;Importjava.security.NoSuchAlgorithmException;Importjava.security.NoSuchProviderException;Importjava.util.ArrayList;ImportJava.util.HashMap;Importjava.util.List;ImportJava.util.Map;ImportJava.util.Map.Entry;ImportJava.util.Random;ImportJava.util.Set;ImportJava.util.TreeMap;Importjavax.net.ssl.HttpsURLConnection;Importjavax.net.ssl.SSLSocketFactory;ImportNet.sf.json.JSONObject;Importorg.apache.commons.codec.digest.DigestUtils;ImportOrg.apache.http.NameValuePair;Importorg.apache.http.client.entity.UrlEncodedFormEntity;ImportOrg.apache.http.client.methods.CloseableHttpResponse;ImportOrg.apache.http.client.methods.HttpGet;ImportOrg.apache.http.client.methods.HttpPost;ImportOrg.apache.http.client.utils.URIBuilder;ImportOrg.apache.http.entity.ContentType;Importorg.apache.http.entity.StringEntity;Importorg.apache.http.impl.client.CloseableHttpClient;Importorg.apache.http.impl.client.HttpClients;ImportOrg.apache.http.message.BasicNameValuePair;ImportOrg.apache.http.util.EntityUtils;

HTTPS sends a POST request [pro-Test available]

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.