Java Processing HTTP Requests

Source: Internet
Author: User

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

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.