1 ImportJava.io.BufferedReader;2 Importjava.io.IOException;3 ImportJava.io.InputStreamReader;4 Importjava.net.HttpURLConnection;5 ImportJava.net.URL;6 8 Importjavax.net.ssl.HttpsURLConnection;Ten One ImportOrg.apache.log4j.Logger; - - /* - * GET request class, non-threading class - */ + Public classGetrequest { - PrivateString url = "Https://b2b.10086.cn/b2b/main/viewNoticeContent.html?noticeBean.id="; + PrivateLogger Logger; A Publicgetrequest () { atLogger = Logger.getlogger (getrequest.class); - } - - Publicextendcandidate getData (String id) { - This. url = url +ID; -BufferedReader in =NULL; inHttpURLConnection conn =NULL; -String result = ""; to Try { +conn = (httpurlconnection)Newurl (url). OpenConnection (); - //The send GET request must have the following two lines set theConn.setdoinput (true); *Conn.setrequestmethod ("GET"); $ //buffer for flush output streamPanax Notoginsengin =NewBufferedReader (NewInputStreamReader (Conn.getinputstream ())); - String Line; the while(line = In.readline ())! =NULL) { +Result + =Line ; A } the}Catch(Exception e) { +Logger.error ("Send GET Request exception!") \ t Request ID: "+id+" \ n "+e.getmessage () +" \ n "); -}finally{//Use the finally block to close the output stream, input stream $ Try { $ if(In! =NULL) { - in.close (); - } the}Catch(IOException ex) { -Logger.error ("Off data out wrong!") \ n "+ex.getmessage () +" \ n ");Wuyi } the } - //Gets the result of results, can be directly added processing, or stored to global variables ... Wu } -}
Java Analog POST request: http://www.cnblogs.com/husky/p/6377553.html
Http-java Analog GET request small chestnut