Java Native Send HTTP request (without any Jar package import)

Source: Internet
Author: User

Tag: MSU stream field TAC buffer request name using user

1  PackageCom.teamsun.pay.wxpay.util;2 3 ImportJava.io.BufferedReader;4 Importjava.io.IOException;5 ImportJava.io.InputStreamReader;6 ImportJava.io.PrintWriter;7 ImportJava.net.URL;8 Importjava.net.URLConnection;9 Importjava.util.List;Ten ImportJava.util.Map; One  A  Public classHttpRequest { -     /** - * Request to send a GET method to the specified URL the      *  -      * @paramURL - * Send the requested URL -      * @paramparam + * Request parameters, request parameters should be in the form of name1=value1&name2=value2.  -      * @returnThe response result of the remote resource represented by the URL +      */ A      Public Staticstring sendget (string url, string param) { atString result = ""; -BufferedReader in =NULL; -         Try { -String urlnamestring = URL + "?" +param; -URL Realurl =NewURL (urlnamestring); -             //opening and linking between URLs inURLConnection connection =realurl.openconnection (); -             //to set common request properties toConnection.setrequestproperty ("Accept", "*/*"); +Connection.setrequestproperty ("Connection", "keep-alive"); -Connection.setrequestproperty ("User-agent", the"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1) "); *             //setconnecttimeout: Setting the connection host timeout (in milliseconds) $Connection.setconnecttimeout (2000);//2-second timeoutPanax Notoginseng             //setreadtimeout: Setting read data timeout from host (in milliseconds) -             //connection.setreadtimeout (+); the              +             //establish an actual connection A Connection.connect (); the             //Get all response header fields +map<string, list<string>> map =connection.getheaderfields (); -             //iterate through all the response header fields $              for(String key:map.keySet ()) { $SYSTEM.OUT.PRINTLN (key + "--->" +Map.get (key)); -             } -             //defines the response of the BufferedReader input stream to read the URL thein =NewBufferedReader (NewInputStreamReader ( - Connection.getinputstream ()));Wuyi String Line; the              while(line = In.readline ())! =NULL) { -Result + =Line ; Wu             } -}Catch(Exception e) { AboutSYSTEM.OUT.PRINTLN ("Send GET request exception!") " +e); $ e.printstacktrace (); -         } -         //Use the finally block to close the input stream -         finally { A             Try { +                 if(In! =NULL) { the in.close (); -                 } $}Catch(Exception E2) { the e2.printstacktrace (); the             } the         } the         returnresult; -     } in      the     /** the * Request to send the Post method to the specified URL About      *  the      * @paramURL the * Send the requested URL the      * @paramparam + * Request parameters, request parameters should be in the form of name1=value1&name2=value2.  -      * @returnThe response result of the remote resource represented the      */Bayi      Public Staticstring sendpost (string url, string param) { thePrintWriter out =NULL; theBufferedReader in =NULL; -String result = ""; -         Try { theURL Realurl =Newurl (url); the             //opening and linking between URLs theURLConnection conn =realurl.openconnection (); the             //to set common request properties -Conn.setrequestproperty ("Accept", "*/*"); theConn.setrequestproperty ("Connection", "keep-alive"); theConn.setrequestproperty ("User-agent", the"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1) ");94             //to send a POST request, you must set the following two lines theConn.setdooutput (true); theConn.setdoinput (true); the             //gets the output stream corresponding to the URLConnection object98out =NewPrintWriter (Conn.getoutputstream ()); About             //Send Request Parameters - out.print (param);101             //buffer for flush output stream102 Out.flush ();103             //defines the response of the BufferedReader input stream to read the URL104in =NewBufferedReader ( the                     NewInputStreamReader (Conn.getinputstream ()));106 String Line;107              while(line = In.readline ())! =NULL) {108Result + =Line ;109             } the}Catch(Exception e) {111SYSTEM.OUT.PRINTLN ("Send POST request exception!") "+e); the e.printstacktrace ();113         } the         //Use the finally block to close the output stream, input stream the         finally{ the             Try{117                 if(out!=NULL){118 out.close ();119                 } -                 if(in!=NULL){121 in.close ();122                 }123             }124             Catch(IOException ex) { the ex.printstacktrace ();126             }127         } -         returnresult;129     } the     131      the      Public Static voidMain (string[] args) {133         134 135     }136 }137 138     

Java Native Send HTTP request (without any Jar package import)

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.