The Java background uses the get postt method to submit parameter classes

Source: Internet
Author: User
Codepackage COM. skyblue. core. database; import Java. io. bufferedreader; import Java. io. ioexception; import Java. io. inputstreamreader; import java.net. httpurlconnection; import java.net. malformedurlexception; import java.net. URL; import java.net. urlconnection; import Java. util. hashmap; import Java. util. iterator; import Java. util. map; public class requesthelper {/***** @ Description: Send a POST method request to a specified URL * @ Method _ Name: sendpost * @ Param linkurl URL * @ Param postdata submit data name1 = value1 & name2 = value2 format. * @ Param headparam common request attributes note setrequestproperty ("cookie", cookievalue) for Cookie writing; * @ return: string * @ creation date: 02:38:59 * @ version: v1.00 * @ Author: skyblue * @ update date: * @ update Author: skyblue */@ suppresswarnings ("unchecked") public static string sendpost (string linkurl, string postdata, map headparam) {string result = ""; Url url = NULL; httpurlconnection httpurl = NULL; bufferedreader reader = NULL; try {url = new URL (linkurl); httpurl = (httpurlconnection) URL. openconnection (); // sets the request method. The default value is posthttpurl. setrequestmethod ("Post"); // post | get // POST requests cannot use cached httpurl. setusecaches (false); // set the general request attribute if (headparam! = NULL) {iterator it = headparam. entryset (). iterator (); While (it. hasnext () {map. entry entry = (map. entry) it. next (); // write the header httpurl. setrequestproperty (string) entry. getkey (), (string) headparam. get (entry. getkey () ;}}// submitted data if (postdata! = NULL &&! "". Equals (postdata) {httpurl. setdooutput (true); httpurl. getoutputstream (). write (postdata. getbytes (); httpurl. getoutputstream (). flush ();} else {httpurl. connect () ;}// obtain all response header fields in Java. util. map <string, Java. util. list <string> map = httpurl. getheaderfields (); // traverses all response header fields for (string key: map. keyset () {system. out. println (Key + "--->" + map. get (key);} int code = httpurl. getresponsecode (); If (code = 20 0) {reader = new bufferedreader (New inputstreamreader (httpurl. getinputstream (); string line; while (line = reader. Readline ())! = NULL) {system. out. println (line); Result + = line ;}} catch (malformedurlexception e) {e. printstacktrace ();} catch (ioexception e) {e. printstacktrace ();} finally {try {If (reader! = NULL) {reader. close () ;}} catch (ioexception ex) {ex. printstacktrace () ;}} return result;}/***** @ Description: Send a get Method Request to a specified URL * @ method_name: sendget * @ Param URL: The request URL * @ Param getdata. The request parameter must be in the format of name1 = value1 & name2 = value2. * @ Param headparam common request attributes note setrequestproperty ("cookie", cookievalue) for Cookie writing; * @ return: string * @ creation date: 02:40:49 * @ version: v1.00 * @ Author: skyblue * @ update date: * @ update Author: skyblue */@ suppresswarnings ("unchecked") public static string sendget (string URL, string getdata, map headparam) {string result = ""; bufferedreader in = NULL; urlconnection Conn = NULL; try {// concatenate the get parameter if (getdata! = NULL &&! "". Equals (getdata) {url = URL + "? "+ Getdata;} URL realurl = new URL (URL); // connection between opening and URL conn = realurl. openconnection (); // set the generic request attribute if (headparam! = NULL) {iterator it = headparam. entryset (). iterator (); While (it. hasnext () {map. entry entry = (map. entry) it. next (); Conn. setrequestproperty (string) entry. getkey (), (string) headparam. get (entry. getkey () ;}}// create the actual connection Conn. connect (); // obtain all response header fields in Java. util. map <string, Java. util. list <string> map = Conn. getheaderfields (); // traverses all response header fields for (string key: map. keyset () {system. out. println (Key + "--->" + Ma P. 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 + = 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 () ;}conn = NULL;} catch (ioexception ex) {ex. printstacktrace () ;}} return result;} // provides the main method to test the public static void main (string ARGs []) function of sending a GET request @ suppresswarnings ("unchecked"). {// send the GET request map = new hashmap (); map. put ("Content-Type", "application/X-WWW-form-urlencoded"); string S1 = requesthelper. sendpost ("http://www.ip138.com/ips.asp", "IP = 119.119.100.121", MAP); system. out. println (S1 );}}

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.