Package COM. example. downloadthread; import Java. io. *; import java.net. *; import Java. util. *;/*** @ author yeeku. h. lee kongyeeku@163.com * @ version 1.0 */public class getpostutil {/*** send a get Method Request to a specified URL ** @ Param URL * send the request URL * @ Param Params * 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 Params) {string result = ""; bufferedreader in = NULL; try {string urlname = URL + "? "+ Params; 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)"); // establish the actual connection Conn. connect (); // obtain all response header fields Map <string, list <string> map = Conn. getheaderfields (); // traverses Some 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 (), "gb2312"); string line; while (line = in. readline ())! = NULL) {result + = "\ n" + line;} catch (exception e) {system. Out. println ("an exception occurred when sending a GET request! "+ E); 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 Params * 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 Params) {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 (C Ompatible; MSIE 6.0; Windows NT 5.1; sv1) "); // The following two lines 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 (Params); // 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 (); string line; while (line = in. readline ())! = NULL) {result + = "\ n" + line;} catch (exception e) {system. Out. println ("an exception occurred when sending a POST request! "+ E); E. printstacktrace () ;}// use finally blocks 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 ;}}