Http-java Analog GET request small chestnut

Source: Internet
Author: User
Tags finally block getmessage

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

Related Article

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.