HTTP authorization Basic Request code sample

Source: Internet
Author: User

/** *  */ PackageTestjava.java;ImportJava.io.BufferedReader;ImportJava.io.InputStream;ImportJava.io.InputStreamReader;Importjava.net.HttpURLConnection;ImportJava.net.URL;Importjava.util.Base64;/** * @authorXXX * @function * @date April 12, 2016 *@version */ Public classZuzuche { Public StaticString Apppost (Object Jsonbean, string requrl,string method)throwsexception{//Send DataHttpURLConnection Conn; Try{URL URL=NewURL (Requrl); Conn=(HttpURLConnection) url.openconnection (); Conn.setdooutput (true); Conn.setdoinput (true); Conn.setusecaches (false); Conn.setinstancefollowredirects (false);//whether redirection is handled automaticallyConn.setrequestmethod (method);//conn.setrequestproperty ("User-agent", "Xxxes_console");//conn.setrequestproperty ("Content-type", "Application/x-gzip");Conn.setrequestproperty ("Host", "xxx.yyy.com"); Conn.setrequestproperty ("Connection", "keep-alive"); //The base64 encoded "USER:PASSWD" string. If not, or if the user's password is incorrect, the HTTP Code 401 page is returned to the client//Authorization: "Basic user name and password base64 encrypted string", note the colon in the middle of the user name and password.Conn.setrequestproperty ("Authorization", "Basic xfdfsfwuxmtc1lwhqqcz9fjpjfdsfsfesow==");                        Conn.connect (); //Receive return DataInputStream in =Conn.getinputstream ();//Gzipinputstream Gzin = new Gzipinputstream (in);//BufferedReader reader = new BufferedReader (new InputStreamReader (Gzin, "UTF-8"));BufferedReader reader =NewBufferedReader (NewInputStreamReader (In, "UTF-8"));            String Line; StringBuffer SB=NewStringBuffer ();  while(line = Reader.readline ())! =NULL) {sb.append (line);            } reader.close ();            Conn.disconnect (); returnsb.tostring (); } Catch(Exception e) {Throwe; }    }         Public Static voidMain (string[] args) {//Base64 Encryption        byte[] auth = Base64.getencoder (). Encode ("User name: Password". GetBytes ()); String Sauth=NewString (auth); System.out.println ("Sauth=" +Sauth); //or: User name: Password @url.com way request, on Firefox browser above.String URL= "Http://xxx.yyy.com/2.0/standard/queryStatistic.php?beginDate=2016-04-01&endDate=2016-04-02"; Try{System.out.println ("Url=" +URL); String result= Apppost (NULL, URL, "GET"); System.err.println ("Interface Call returns Result:" +result); } Catch(Exception e) {//TODO auto-generated Catch blockE.printstacktrace (); }    }}

HTTP authorization Basic Request code sample

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.