Java sends get and POST requests

Source: Internet
Author: User

1  PackageCom.baqingshe.bjs.util;2 3 ImportJava.io.BufferedReader;4 5 Importjava.io.IOException;6 7 ImportJava.io.InputStream;8 9 ImportJava.io.InputStreamReader;Ten  One ImportJava.io.PrintWriter; A  - ImportJava.net.URL; -  the Importjava.net.URLConnection; -  - Importjava.util.List; -  + ImportJava.util.Map; -  + Importsun.net.www.protocol.http.HttpURLConnection; A  at  Public classHttpRequest { -  -    Public StaticString doget (string url)throwsException { -  -URL Localurl =Newurl (url); -  inURLConnection connection =localurl.openconnection (); -  toHttpURLConnection httpurlconnection =(httpurlconnection) connection; +  -         the  *Httpurlconnection.setrequestproperty ("Accept-charset", "Utf-8"); $ Panax NotoginsengHttpurlconnection.setrequestproperty ("Content-type", "Application/json"); -  the         +  AInputStream InputStream =NULL; the  +InputStreamReader InputStreamReader =NULL; -  $BufferedReader reader =NULL; $  -StringBuffer Resultbuffer =NewStringBuffer (); -  theString Templine =NULL; - Wuyi         the  -        if(Httpurlconnection.getresponsecode () >= 300) { Wu  -            Throw NewException ("HTTP Request isn't success, Response code is" +Httpurlconnection.getresponsecode ()); About  $        } -  -         -  A        Try { +  theInputStream =Httpurlconnection.getinputstream (); -  $InputStreamReader =NewInputStreamReader (inputstream); the  theReader =NewBufferedReader (inputstreamreader); the  the             -  in             while((Templine = Reader.readline ())! =NULL) { the  the resultbuffer.append (templine); About  the            } the  the             +  -}finally { the Bayi             the  the            if(Reader! =NULL) { -  - reader.close (); the  the            } the  the             -  the            if(InputStreamReader! =NULL) { the  the inputstreamreader.close ();94  the            } the  the            98  About            if(InputStream! =NULL) { - 101 inputstream.close ();102 103            }104  the            106 107        }108 109         the 111        returnresultbuffer.tostring (); the 113    } the  the     the 117      Public Staticstring sendpost (string url, string param) {118 119PrintWriter out =NULL; - 121BufferedReader in =NULL;122 123String result = "";124  the         Try {126 127URL Realurl =Newurl (url); - 129             //opening and linking between URLs the 131URLConnection conn =realurl.openconnection (); the 133             //to set common request properties134 135Conn.setrequestproperty ("Accept", "*/*");136 137Conn.setrequestproperty ("Connection", "keep-alive");138 139Conn.setrequestproperty ("User-agent", $ 141"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1) ");142 143             //to send a POST request, you must set the following two lines144 145Conn.setdooutput (true);146 147Conn.setdoinput (true);148 149             //gets the output stream corresponding to the URLConnection object Max 151out =NewPrintWriter (Conn.getoutputstream ()); the 153             //Send Request Parameters154 155 out.print (param);156 157             //buffer for flush output stream158 159 Out.flush (); the 161             //defines the response of the BufferedReader input stream to read the URL162 163in =NewBufferedReader (164 165                     NewInputStreamReader (Conn.getinputstream ()));166 167 String Line;168 169              while(line = In.readline ())! =NULL) { the 171Result + =Line ;172 173             }174 175}Catch(Exception e) {176 177SYSTEM.OUT.PRINTLN ("Send POST request exception!") "+e);178 179 e.printstacktrace (); the 181         }182 183         //Use the finally block to close the output stream, input stream184 185         finally{186 187             Try{188 189                 if(out!=NULL){ the 191 out.close ();192 193                 }194 195                 if(in!=NULL){196 197 in.close ();198 199                 } $ 201             }202 203             Catch(IOException ex) {204 205 ex.printstacktrace ();206 207             }208 209         } About 211         returnresult;212 213     }    214 215}

Java sends get and POST requests

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.