Java tool-send GET/POST request Tool

Source: Internet
Author: User

Import Java. io. bufferedreader; import Java. io. ioexception; import Java. io. inputstream; import Java. io. inputstreamreader; import java.net. httpurlconnection; import java.net. URL; import Java. NIO. charset. charset; import Java. util. map; import Java. util. vector;/*** Java HTTP request object tool class for sending get/POST requests */public class httprequester {private string defaultcontentencoding; Public httprequester () {This. defaultcontentencodi Ng = charset. defaultcharset (). name ();}/*** send GET request ** @ Param urlstring URL address * @ return response object * @ throws ioexception */Public httprespons sendget (string urlstring) throws ioexception {return this. send (urlstring, "get", null, null );} /*** send GET request *** @ Param urlstring URL address * @ Param Params parameter set * @ return response object * @ throws ioexception */Public httprespons sendget (string urlstring, map <string, string> par AMS) throws ioexception {return this. send (urlstring, "get", Params, null );} /*** send a GET request ** @ Param urlstring URL * @ Param Params parameter set * @ Param propertys request attribute * @ return response object * @ throws ioexception */Public httprespons sendget (string urlstring, map <string, string> Params, Map <string, string> propertys) throws ioexception {return this. send (urlstring, "get", Params, propertys);}/*** send POST request ** @ Pa Ram urlstring URL * @ return response object * @ throws ioexception */Public httprespons sendpost (string urlstring) throws ioexception {return this. send (urlstring, "Post", null, null );} /*** send POST request *** @ Param urlstring URL address * @ Param Params parameter set * @ return response object * @ throws ioexception */Public httprespons sendpost (string urlstring, map <string, string> Params) throws ioexception {return this. send (urlstring, "P Ost ", Params, null );} /*** send POST request *** @ Param urlstring URL address * @ Param Params parameter set * @ Param propertys request attribute * @ return response object * @ throws ioexception */Public httprespons sendpost (string urlstring, map <string, string> Params, Map <string, string> propertys) throws ioexception {return this. send (urlstring, "Post", Params, propertys);}/*** send http request ** @ Param urlstring address * @ Param method get/Post * @ Param Parameters adds the request parameter * @ Param propertys specified by the key-value pair to the general request attribute * @ return ing object * @ throws ioexception */private httprespons send (string urlstring, string method, Map <string, string> parameters, Map <string, string> propertys) throws ioexception {httpurlconnection urlconnection = NULL; If (method. equalsignorecase ("get") & parameters! = NULL) {stringbuffer Param = new stringbuffer (); int I = 0; For (string key: parameters. keyset () {if (I = 0) Param. append ("? "); Elseparam. append ("&"); Param. append (key ). append ("= "). append (parameters. get (key); I ++;} urlstring + = Param;} URL url = new URL (urlstring); urlconnection = (httpurlconnection) URL. openconnection (); urlconnection. setrequestmethod (method); urlconnection. setdooutput (true); urlconnection. setdoinput (true); urlconnection. setusecaches (false); If (propertys! = NULL) for (string key: propertys. keyset () {urlconnection. addrequestproperty (Key, propertys. get (key);} If (method. equalsignorecase ("Post") & parameters! = NULL) {stringbuffer Param = new stringbuffer (); For (string key: parameters. keyset () {Param. append ("&"); Param. append (key ). append ("= "). append (parameters. get (key);} urlconnection. getoutputstream (). write (Param. tostring (). getbytes (); urlconnection. getoutputstream (). flush (); urlconnection. getoutputstream (). close ();} return this. makecontent (urlstring, urlconnection);}/*** get response object ** @ Param urlconnectio N * @ return response object * @ throws ioexception */private httprespons makecontent (string urlstring, httpurlconnection urlconnection) throws ioexception {httprespons httpresponser = new httprespons (); try {inputstream in = urlconnection. getinputstream (); bufferedreader = new bufferedreader (New inputstreamreader (in); httpresponser. contentcollection = new vector <string> (); stringbuffer temp = new Stringbuffer (); string line = bufferedreader. Readline (); While (line! = NULL) {httpresponser. contentcollection. add (line); temp. append (line ). append ("\ r \ n"); line = bufferedreader. readline ();} bufferedreader. close (); string ecod = urlconnection. getcontentencoding (); If (ecod = NULL) ecod = This. defaultcontentencoding; httpresponser. urlstring = urlstring; httpresponser. defaultport = urlconnection. geturl (). getdefaultport (); httpresponser. file = urlconnection. geturl (). getFile (); H Ttpresponser. host = urlconnection. geturl (). gethost (); httpresponser. path = urlconnection. geturl (). getpath (); httpresponser. port = urlconnection. geturl (). getport (); httpresponser. protocol = urlconnection. geturl (). getprotocol (); httpresponser. query = urlconnection. geturl (). getquery (); httpresponser. ref = urlconnection. geturl (). getref (); httpresponser. userinfo = urlconnection. geturl (). getuserinfo (); httpres Ponser. content = new string (temp. tostring (). getbytes (), ecod); httpresponser. contentencoding = ecod; httpresponser. code = urlconnection. getresponsecode (); httpresponser. message = urlconnection. getresponsemessage (); httpresponser. contenttype = urlconnection. getcontenttype (); httpresponser. method = urlconnection. getrequestmethod (); httpresponser. connecttimeout = urlconnection. getconnecttimeout (); httprespo Nser. readtimeout = urlconnection. getreadtimeout (); Return httpresponser;} catch (ioexception e) {Throw E;} finally {If (urlconnection! = NULL) urlconnection. disconnect () ;}/ *** default Response Character Set */Public String getdefacontcontentencoding () {return this. defaultcontentencoding;}/*** set the default Response Character Set */Public void setdefacontcontentencoding (string defaultcontentencoding) {This. defaultcontentencoding = defaultcontentencoding ;}}

Transferred from [http://www.open-open.com/lib/view/open1374585832808.html]

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.