1 ImportCom.diyfintech.wx.service.HttpService;2 ImportOrg.springframework.stereotype.Service;3 4 ImportJava.io.BufferedReader;5 ImportJava.io.InputStream;6 ImportJava.io.InputStreamReader;7 ImportJava.io.OutputStream;8 Importjava.net.HttpURLConnection;9 ImportJava.net.URL;Ten @Service One Public classHttpserviceimplImplementsHttpservice { A - //process HTTP request requesturl for request address Requestmethod request, value "GET" or "POST" - Publicstring HttpRequest (String requesturl,string requestmethod,string outputstr) { the -StringBuffer buffer=NULL; - Try{ -URL url=NewURL (requesturl); +HttpURLConnection conn=(HttpURLConnection) url.openconnection (); -Conn.setdooutput (true); +Conn.setdoinput (true); A Conn.setrequestmethod (requestmethod); at Conn.connect (); - //writing to the server is the parameter required to initiate an HTTP request - if(NULL!=outputstr) { -OutputStream os=Conn.getoutputstream (); -Os.write (Outputstr.getbytes ("Utf-8")); - os.close (); in } - //read the contents returned by the server side toInputStream is=Conn.getinputstream (); +InputStreamReader isr=NewInputStreamReader (IS, "Utf-8"); -BufferedReader br=NewBufferedReader (ISR); theBuffer=NewStringBuffer (); *String line=NULL; $ while((Line=br.readline ())! =NULL){Panax Notoginseng Buffer.append (line); - } the}Catch(Exception e) { + e.printstacktrace (); A } the returnbuffer.tostring (); + } - $}
Java Processing HTTP Requests