Java sends get/POST requests

Source: Internet
Author: User

The following example shows how to send a GET request and a POST request to a Web site and obtain a response from the Web site:

Package Your. pack; import Java. io. bufferedreader; import Java. io. ioexception; import Java. io. inputstreamreader; import Java. io. printwriter; import java.net. URL; import java.net. urlconnection; import Java. util. list; import Java. util. map; import android. util. log; public class connutil {// MySQL enables external database connection // grant all privileges on *. * To 'admin' @ '10. 22.69.182 'identified by '000000' with grant option; // flush privi Leges; // modify validity // public static final string base_url = "http: // 127.0.0.1: 808/mywebsite_01/inserttmpservlet"; public static final string base_url = "http: /// 10.22.69.179: 808/mywebsite_01/inserttmpservlet "; /*** send a get Method Request to a specified URL ** @ Param URL * send the request URL * @ Param * request parameter, the request parameters should be in the form of name1 = value1 & name2 = value2. * @ Return URL indicates the response of the Remote resource */public static string sendget (string URL, string PARAM) {string result = ""; bufferedreader in = NULL; try {string urlname = URL + "? "+ Param; // log. I ("connutil", urlname); system. out. println (urlname); Url realurl = new URL (urlname); // enable the URL Connection urlconnection conn = realurl. openconnection (); // set the common request attribute Conn. setrequestproperty ("accept", "*/*"); Conn. setrequestproperty ("connection", "keep-alive"); Conn. setrequestproperty ("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; sv1)"); // set the timeout value Conn. setconnecttimeout (10000); con N. setreadtimeout (10000); // create the actual connection Conn. connect (); // obtain all response header fields Map <string, list <string> map = Conn. getheaderfields (); // traverses all response header fields for (string key: map. keyset () {system. out. println (Key + "--->" + map. get (key);} // defines the bufferedreader input stream to read the URL response in = new bufferedreader (New inputstreamreader (Conn. getinputstream (); string line; while (line = in. readline ())! = NULL) {result + = "\ n" + line;} catch (exception e) {result = "Get: server connection failed! "; E. printstacktrace () ;}// use the Finally block to close the input stream finally {try {If (in! = NULL) {In. close () ;}} catch (ioexception ex) {ex. printstacktrace () ;}} return result;}/*** send a POST method request to a specified URL ** @ Param URL * The request URL * @ Param * request parameter, the request parameters should be in the form of name1 = value1 & name2 = value2. * @ Return URL indicates the response of the Remote resource */public static string sendpost (string URL, string PARAM) {printwriter out = NULL; bufferedreader in = NULL; string result = ""; try {URL realurl = new URL (URL); // enable the URL Connection urlconnection conn = realurl. openconnection (); // set the common request attribute Conn. setrequestproperty ("accept", "*/*"); Conn. setrequestproperty ("connection", "keep-alive"); Conn. setrequestproperty ("User-Agent", "Mozilla/4.0 (Co Mpatible; MSIE 6.0; Windows NT 5.1; sv1) "); // set the timeout value Conn. setconnecttimeout (10000); Conn. setreadtimeout (10000); // The following two rows of conn must be set to send a POST request. setdooutput (true); Conn. setdoinput (true); // get the output stream of the urlconnection object out = new printwriter (Conn. getoutputstream (); // sends the request parameter out. print (PARAM); // flush the Buffer out of the output stream. flush (); // defines the bufferedreader input stream to read the URL response in = new bufferedreader (New inputstreamreader (Conn. getinputstream (); ST Ring Line; while (line = in. Readline ())! = NULL) {result + = "\ n" + line;} catch (exception e) {result = "post: server connection failed! "; E. printstacktrace () ;}// use the Finally block to close the output stream and input stream finally {try {If (OUT! = NULL) {out. Close () ;}if (in! = NULL) {in. Close () ;}catch (ioexception ex) {ex. printstacktrace () ;}} return result ;}}

When sending a GET request in the above program, you only need to put the request parameters after the URL string? The program can directly call the Connect Method of the urlconnection object. If the program needs to send a POST request, you must first set the values of the doin and doout request header fields, use the output stream corresponding to urlconnection to send request parameters.

Whether sending a GET request or a POST request, the program obtains the urlconnection response in the same way: if the program can determine that the remote response is a response stream, it can use the response stream to read the response; if the program cannot determine that the remote response is a response stream, use the byte stream to read the response.

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.