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