ImportJava.io.BufferedReader;ImportJava.io.InputStream;ImportJava.io.InputStreamReader;ImportJava.io.PrintWriter;ImportJava.net.Authenticator;Importjava.net.InetSocketAddress;Importjava.net.PasswordAuthentication;ImportJava.net.Proxy;ImportJava.net.URL;Importjava.net.URLConnection; Public classHttputil {/** * * @paramurlstring Request URL *@paramRes returns results *@paramTimeout time-out (min) *@paramUseProxy whether to use a proxy *@return * @throwsException*/ Public StaticString Sendget (String urlstring, String res, String timeOut,BooleanUseProxy)throwsException {URL url=NewURL (urlstring); URLConnection UC=NULL; //deciding whether to use a proxy if(useproxy) {//using proxiesInetsocketaddress isa =NewInetsocketaddress (PropertiesUtil.properties.getProperty ("Proxy_host"), Integer.parseint ( PropertiesUtil.properties.getProperty ("Proxy_port"))); Proxy proxy=NewProxy (Proxy.Type.HTTP, ISA); Authenticator.setdefault (NewMyauthenticator (PropertiesUtil.properties.getProperty ("Proxy_user"), PropertiesUtil.properties.getProperty (" Proxy_password "))); UC=url.openconnection (proxy); }Else{ //No proxyUC =url.openconnection (); } uc.setrequestproperty ("Content-type", "Text/plain; Charset=utf-8 "); Uc.setreadtimeout (60 * 1000 *Integer.parseint (TimeOut)); Uc.setdooutput (true); Uc.setdoinput (true); InputStream is=Uc.getinputstream (); intC; StringBuffer SF=NewStringBuffer (); while((c = Is.read ())! = 1) {sf.append (Char) c); } byte[] B2 = sf.tostring (). GetBytes ("Iso8859_1"); String SS=NewString (B2, "UTF-8"); Res=SS; Is.close (); returnRes; } Public StaticString Sendpost (String urlstring, string param, string res, String timeOut,BooleanUseProxy)throwsException {PrintWriter out=NULL; BufferedReader in=NULL; URLConnection Conn=NULL; String result= ""; URL Realurl=NewURL (urlstring); //deciding whether to use a proxy if(useproxy) {//using proxiesInetsocketaddress isa =NewInetsocketaddress (PropertiesUtil.properties.getProperty ("Proxy_host"), Integer.parseint ( PropertiesUtil.properties.getProperty ("Proxy_port"))); Proxy proxy=NewProxy (Proxy.Type.HTTP, ISA); Authenticator.setdefault (NewMyauthenticator (PropertiesUtil.properties.getProperty ("Proxy_user"), PropertiesUtil.properties.getProperty (" Proxy_password "))); Conn=realurl.openconnection (proxy); }Else{ //opening and linking between URLsconn =realurl.openconnection (); } //to set common request propertiesConn.setrequestproperty ("Accept", "*/*"); Conn.setrequestproperty ("Connection", "keep-alive"); Conn.setrequestproperty ("Content-type", "Text/plain; Charset=utf-8 "); Conn.setrequestproperty ("User-agent", "mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1) "); //to send a POST request, you must set the following two linesConn.setdooutput (true); Conn.setdoinput (true); //gets the output stream corresponding to the URLConnection objectout =NewPrintWriter (Conn.getoutputstream ()); //Send Request Parametersout.print (param); //buffer for flush output streamOut.flush (); //defines the response of the BufferedReader input stream to read the URLin =NewBufferedReader (NewInputStreamReader (Conn.getinputstream ())); String Line; while(line = In.readline ())! =NULL) {result+=Line ; } //Use the finally block to close the output stream, input stream if(out!=NULL) {out.close (); } if(in!=NULL) {in.close (); } returnresult; } Public Static classMyauthenticatorextendsAuthenticator {PrivateString user = ""; PrivateString Password = ""; Publicmyauthenticator (string user, string password) { This. user =user; This. Password =password; } protectedpasswordauthentication getpasswordauthentication () {return Newpasswordauthentication (User, Password.tochararray ()); } } Public Static voidMain (string[] args)throwsException {//System.out.println (Httputil.sendget ("http://www.baidu.com" ," "," 5 ", true));System.out.println (Httputil.sendpost ("http://www.baidu.com", "" "," "," 5 ",true)); }}
Java HTTP native Get and post support agent authentication