Two access interface methods (get and post)

Source: Internet
Author: User

There are three types of cross-machine and cross-language Remote Access: scoket sends data packets, HTTP sends requests, and RMI remote connections;

HTTP request sending Methods: Post and get

Importjava. io. ioexception; importjava. io. inputstream; import Java. util. map; importjava. util. concurrent. atomic. atomicinteger; importorg. apache. commons. httpclient. httpclient; importorg. apache. commons. httpclient. httpexception; importorg. apache. commons. httpclient. httpstatus; importorg. apache. commons. httpclient. multithreadedhttpconnectionmanager; importorg. apache. commons. httpclient. namevaluepair; importorg. apach E. commons. httpclient. methods. getmethod; importorg. apache. commons. httpclient. methods. postmethod; importorg. apache. commons. httpclient. params. httpconnectionmanagerparams; importorg. apache. commons. io. ioutils; import Org. slf4j. logger; import Org. slf4j. loggerfactory; publicclass sendhttpurl {privatefinalstatic logger = loggerfactory. getlogger (sendhttpurl. class); privatestatic httpclient = N Ull; strong connectionmanager = NULL; // multithreading manager privateintmaxthreadstotal = 128; // maximum number of threads privateintmaxthreadsperhost = 32; // maximum number of threads allocated to each client privateintconnectiontimeout = 15000; // connection timeout, millisecond privateintsotimeout = 14000; // data read timeout, millisecond publicvoid Init () {connectionmanager = newmultithreadedhttpconnectionmanager (); httpconnectionmanagerparams Params = newhttpco Nnectionmanagerparams (); Params. setconnectiontimeout (connectiontimeout); Params. setmaxtotalconnections (maxthreadstotal); Params. setsotimeout (sotimeout); If (maxthreadstotal> maxthreadsperhost) {Params. setdefamaxmaxconnectionsperhost (maxthreadsperhost);} else {Params. setdefamaxmaxconnectionsperhost (maxthreadstotal);} connectionmanager. setparams (Params); httpclient = new httpclient (connectionma Nager); httpclient. gethttpconnectionmanager (). getparams (). setconnectiontimeout (connectiontimeout); httpclient. gethttpconnectionmanager (). getparams (). setsotimeout (sotimeout);}/*** get method access ** @ Param URL * @ Param calltype * @ Param parmmap * @ return */private string callbyget (stringurl, string calltype, map <string, string> parmmap) {If (logger. isdebugenabled () {logger. debug ("in get, URL:" + URL) ;} Getmethod = new getmethod (URL); int statuscode = 0; try {statuscode = httpclient.exe cutemethod (getmethod); If (logger. isdebugenabled () {logger. debug ("inget, statuscode:" + statuscode);} If (statuscode! = Httpstatus. SC _ OK) {// logger. Error ("in get, failed to access appagent. Network problems. Statuscode: "+ statuscode); returnnull;} inputstream = getmethod. getresponsebodyasstream (); If (inputstream = NULL) {// access is normal: the obtained data is empty logger. error ("in get, data returned from appagent is empty! "); Returnnull;} string RTN = ioutils. tostring (inputstream, "UTF-8"); Return RTN;} catch (httpexception e) {logger. error ("failed to access appagent in get mode! ", E); returnnull;} catch (ioexception e) {logger. Error (" failed to access appagent in get mode! ", E); returnnull;} finally {getmethod. releaseconnection () ;}}/*** post access ** @ Param URL * @ Param calltype * @ Param parmmap * @ return */private string callbypost (stringurl, string calltype, map <string, string> parmmap) {If (logger. isdebugenabled () {logger. debug ("inpost, URL:" + URL);} postmethod P = new postmethod (URL); If (parmmap! = NULL) {namevaluepair [] Params = newnamevaluepair [parmmap. size ()]; atomicinteger = new atomicinteger (0); For (map. entry <string, string> parm: parmmap. entryset () {namevaluepair parmvalue = newnamevaluepair (parm. getkey (), parm. getvalue (); Params [atomicinteger. getandincrement ()] = parmvalue;} p. setrequestbody (Params);} Try {int statuscode = httpclient.exe cutemethod (p); logger. deb UG ("inget, statuscode:" + statuscode); If (statuscode! = Httpstatus. SC _ OK) {// exception logger. Error ("in post, access to appagent failed. Network problems. Statuscode: "+ statuscode); returnnull;} inputstream = P. getresponsebodyasstream (); If (inputstream = NULL) {// access the logger normally. error ("in post, data returned from appagent is empty! "); Returnnull;} string RTN = ioutils. tostring (inputstream, "UTF-8"); Return RTN;} catch (httpexception e) {logger. error ("appagent Access failed in post mode! ", E); returnnull;} catch (ioexception e) {logger. Error (" failed to access appagent in post mode! ", E); returnnull ;}finally {P. releaseconnection ();}}}



Please specify: blog.csdn.net/yangkai_hudong

Related Article

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.