The author has made a small system accessibility feature that can periodically access a URL, execute an SQL statement, or execute a system command.
The execution of SQL statements and system commands is simpler, and there is no more detail here, mainly to access a URL.
In fact, the JDK itself already has a tool class to create HTTP requests, the class name is: Java.net.HttpURLConnection, but given that the basic classes are usually rough, many cases to consider and deal with, they turned to Google, and found that there is an open source toolkit can be used , several toolkit in order to httpclient more commonly used, and is Apache Dongdong, so decided to use HttpClient.
From Apache down the package Commons-httpclient-3.1.jar and Commons-codec-1.3.jar two packages, which are httpclient dependent packages.
Help to write very well, even if it is like me, English sucks, also can quickly start.
public boolean visiturl (String url) {
//Commons httpclient 3.1
HttpClient client = new HttpClient ( );
HttpMethod method = new GetMethod (URL);
//provide custom retry handler is necessary
Method.getparams (). Setparameter (Httpmethodparams.retry_ HANDLER, New Defaulthttpmethodretryhandler (3, false));
Boolean rs = false;
try {
//Execute the method.
int statusCode = Client.executemethod (method);
if (StatusCode!= httpstatus.sc_ok) {
Logger.error ("Method failed:" + method.getstatusline ());
}
Else {
rs = true;
}
catch (HttpException e) {
Logger.error ("Fatal protocol violation:" + e.getmessage ());
} catch (IOException e) {
Logger.error ("Fatal Transport Error:" + e.getmessage ());
finally {
//release the connection.
Method.releaseconnection ();
}
return rs;
}
Native Tomcat run, work properly, and then drop to the server (Oracle IAS environment) test, the program should unexpectedly reported a mistake.
09/03/16 19:03:43 Java.lang.NoClassDefFoundError
09/03/16 19:03:43 at Org.apache.commons.httpclient.HttpMethodBase.writeRequestLine (httpmethodbase.java:2015)
09/03/16 19:03:43 at Org.apache.commons.httpclient.HttpMethodBase.writeRequest (httpmethodbase.java:1864)
09/03/16 19:03:43 at Org.apache.commons.httpclient.HttpMethodBase.execute (httpmethodbase.java:975)
09/03/16 19:03:43 at Org.apache.commons.httpclient.HttpMethodDirector.executeWithRetry (Httpmethoddirector.java : 368)
09/03/16 19:03:43 at Org.apache.commons.httpclient.HttpMethodDirector.executeMethod (httpmethoddirector.java:164)
09/03/16 19:03:43 at Org.apache.commons.httpclient.HttpClient.executeMethod (httpclient.java:437)
09/03/16 19:03:43 at Org.apache.commons.httpclient.HttpClient.executeMethod (httpclient.java:324)
09/03/16 19:03:43 at Com.zbht.util.TimerTaskManager.runURLTask (timertaskmanager.java:237)
09/03/16 19:03:43 at _system._timer__task._test._jspservice (_test.java:182)
09/03/16 19:03:43 at Com.orionserver.http.OrionHttpJspPage.service (orionhttpjsppage.java:59)
09/03/16 19:03:43 at Oracle.jsp.runtimev2.JspPageTable.service (jsppagetable.java:462)
09/03/16 19:03:43 at Oracle.jsp.runtimev2.JspServlet.internalService (jspservlet.java:594)
09/03/16 19:03:43 at Oracle.jsp.runtimev2.JspServlet.service (jspservlet.java:518)
09/03/16 19:03:43 at Javax.servlet.http.HttpServlet.service (httpservlet.java:856)
09/03/16 19:03:43 at Com.evermind.server.http.ServletRequestDispatcher.invoke (servletrequestdispatcher.java:713)
09/03/16 19:03:43 at Com.evermind.server.http.ServletRequestDispatcher.forwardInternal ( servletrequestdispatcher.java:370)
09/03/16 19:03:43 at Com.evermind.server.http.HttpRequestHandler.doProcessRequest (httprequesthandler.java:871)
09/03/16 19:03:43 at Com.evermind.server.http.HttpRequestHandler.processRequest (httprequesthandler.java:453)
09/03/16 19:03:43 at Com.evermind.server.http.AJPRequestHandler.run (ajprequesthandler.java:302)
09/03/16 19:03:43 at Com.evermind.server.http.AJPRequestHandler.run (ajprequesthandler.java:190)
09/03/16 19:03:43 at Oracle.oc4j.network.serversocketreadhandler$saferunnable.run (ServerSocketReadHandler.java : 260)
09/03/16 19:03:43 at Com.evermind.util.releasableresourcepooledexecutor$myworker.run ( releasableresourcepooledexecutor.java:303)
09/03/16 19:03:43 at Java.lang.Thread.run (thread.java:595)