<%@ page import="org.apache.commons.httpclient.HttpClient" %> <%@ page import="org.apache.commons.httpclient.UsernamePasswordCredentials" %> <%@ page import="org.apache.commons.httpclient.auth.AuthScope" %> <%@ page import="org.apache.commons.httpclient.methods.GetMethod" %> Sample URL Fetcher

Sample HTTP Fetcher


<% String resultString = "No Content"; HttpClient client = new HttpClient(); // pass our credentials to HttpClient, they will only be used for // authenticating to servers with realm "Alfresco" on the host // "localhost", to authenticate against // an arbitrary realm or host change the appropriate argument to null. client.getState().setCredentials( new AuthScope("localhost", 8080, "Alfresco"), new UsernamePasswordCredentials("admin", "admin") ); // create a GET method that reads a file over HTTPS, we're assuming // that this file requires basic authentication using the realm above. GetMethod get = new GetMethod("http://localhost:8080/alfresco/s/sample/folder/Company%20Home"); // Tell the GET method to automatically handle authentication. The // method will use any appropriate credentials to handle basic // authentication requests. Setting this value to false will cause // any request for authentication to return with a status of 401. // It will then be up to the client to handle the authentication. get.setDoAuthentication( true ); try { // execute the GET int status = client.executeMethod( get ); // get the response resultString = get.getResponseBodyAsString(); } finally { // release any connection resources used by the method get.releaseConnection(); } %> <%=resultString%>