/** * */ 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