List of usage examples for org.apache.commons.httpclient UsernamePasswordCredentials UsernamePasswordCredentials
public UsernamePasswordCredentials(String paramString1, String paramString2)
From source file:BasicAuthenticationForJSPPage.java
public static void main(String args[]) throws Exception { HttpClient client = new HttpClient(); client.getParams().setParameter("http.useragent", "My Browser"); HostConfiguration host = client.getHostConfiguration(); host.setHost(new URI("http://localhost:8080", true)); Credentials credentials = new UsernamePasswordCredentials("tomcat", "tomcat"); AuthScope authScope = new AuthScope(host.getHost(), host.getPort()); HttpState state = client.getState(); state.setCredentials(authScope, credentials); GetMethod method = new GetMethod("/commons/chapter01/protected.jsp"); try {//w ww .ja v a 2 s .com client.executeMethod(host, method); System.err.println(method.getStatusLine()); System.err.println(method.getResponseBodyAsString()); } catch (Exception e) { System.err.println(e); } finally { method.releaseConnection(); } }
From source file:Correct.java
public static void main(String[] args) { String URLL = ""; HttpClient client = new HttpClient(); HttpMethod method = new PostMethod(URLL); method.setDoAuthentication(true);/*w ww .jav a 2 s . c om*/ HostConfiguration hostConfig = client.getHostConfiguration(); hostConfig.setHost("172.29.38.8"); hostConfig.setProxy("172.29.90.4", 8); // NTCredentials proxyCredentials = new NTCredentials("", "", "", ""); client.getState().setCredentials(AuthScope.ANY, new UsernamePasswordCredentials("", "")); //// try { //// URL url = new URL(""); //// HttpURLConnection urls = (HttpURLConnection) url.openConnection(); //// //// //// } catch (MalformedURLException ex) { //// Logger.getLogger(Correct.class.getName()).log(Level.SEVERE, null, ex); //// } catch (IOException ex) { //// Logger.getLogger(Correct.class.getName()).log(Level.SEVERE, null, ex); //// } try { // send the transaction client.executeMethod(hostConfig, method); StatusLine status = method.getStatusLine(); if (status != null && method.getStatusCode() == 200) { System.out.println(method.getResponseBodyAsString() + "\n Status code : " + status); } else { System.err.println(method.getStatusLine() + "\n: Posting Failed !"); } } catch (IOException ioe) { ioe.printStackTrace(); } method.releaseConnection(); }
From source file:com.discursive.jccook.slide.ListExample.java
public static void main(String[] args) throws HttpException, IOException { String url = "http://www.discursive.com/jccook/dav/"; Credentials credentials = new UsernamePasswordCredentials("davuser", "davpass"); // List resources in top directory WebdavResource resource = new WebdavResource(url, credentials); WebdavResource[] resources = resource.listWebdavResources(); System.out.println("type name size type modified"); System.out.println("--------------------------------------------------------------------"); for (int i = 0; i < resources.length; i++) { WebdavResource item = resources[i]; String type;//ww w .ja va 2s .c o m if (item.isCollection()) { type = "dir"; } else { type = "file"; } System.out.print(StringUtils.rightPad(type, 6)); System.out.print(StringUtils.rightPad(item.getName(), 15)); System.out.print(StringUtils.rightPad(item.getGetContentLength() + "", 8)); System.out.print(StringUtils.rightPad(item.getGetContentType(), 23)); Date lastMod = new Date(item.getGetLastModified()); System.out.print(StringUtils.rightPad(FastDateFormat.getInstance().format(lastMod), 25)); System.out.print("\n"); } }
From source file:com.discursive.jccook.slide.ModifyExample.java
public static void main(String[] args) throws HttpException, IOException { HttpClient client = new HttpClient(); String url = "http://www.discursive.com/jccook/dav/test.html"; Credentials credentials = new UsernamePasswordCredentials("davuser", "davpass"); // List resources in top directory WebdavResource resource = new WebdavResource(url, credentials); // Lock the Resource for 100 seconds boolean locked = resource.lockMethod("tobrien", 100); if (locked) { try {/* ww w . ja va2s .c o m*/ // Read content as a String String resourceData = resource.getMethodDataAsString(); printResource(resource, resourceData); // Modify a resource System.out.println("*** Modifying Resource"); resourceData = resourceData.replaceAll("test", "modified test"); resource.putMethod(resourceData); } finally { // Unlock the resource resource.unlockMethod("tobrien"); } } // Close the resource resource.close(); }
From source file:com.discursive.jccook.slide.GetExample.java
public static void main(String[] args) throws HttpException, IOException { HttpClient client = new HttpClient(); String url = "http://www.discursive.com/jccook/dav/test.html"; Credentials credentials = new UsernamePasswordCredentials("davuser", "davpass"); // List resources in top directory WebdavResource resource = new WebdavResource(url, credentials); System.out.println("The three ways to Read resources."); // Read to a temporary file File tempFile = new File(resource.getName()); resource.getMethod(tempFile);//from w w w . j a v a 2 s . c o m System.out.println("1. " + resource.getName() + " saved in file: " + tempFile.toString()); // Read as a String String resourceData = resource.getMethodDataAsString(); System.out.println("2. Contents of " + resource.getName() + " as String."); System.out.println(resourceData); // Read with a stream InputStream resourceStream = resource.getMethodData(); Reader reader = new InputStreamReader(resourceStream); StringWriter writer = new StringWriter(); while (reader.ready()) { writer.write(reader.read()); } System.out.println("3. Contents of " + resource.getName() + " from InputStream."); System.out.println(writer.toString()); resource.close(); }
From source file:com.discursive.jccook.httpclient.BasicAuthExample.java
public static void main(String[] args) throws HttpException, IOException { // Configure Logging System.setProperty("org.apache.commons.logging.Log", "org.apache.commons.logging.impl.SimpleLog"); System.setProperty("org.apache.commons.logging.simplelog.showdatetime", "true"); System.setProperty("org.apache.commons.logging.simplelog.log.httpclient.wire", "debug"); System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.commons.httpclient", "debug"); HttpClient client = new HttpClient(); HttpState state = client.getState(); HttpClientParams params = client.getParams(); // Set credentials on the client Credentials credentials = new UsernamePasswordCredentials("testuser", "crazypass"); state.setCredentials(null, null, credentials); params.setAuthenticationPreemptive(true); String url = "http://www.discursive.com/jccook/auth/"; HttpMethod method = new GetMethod(url); client.executeMethod(method);// w w w . j ava 2s . co m String response = method.getResponseBodyAsString(); System.out.println(response); method.releaseConnection(); }
From source file:com.francetelecom.admindm.changedustate.callviaacs.CallChangeDUStateUninstallCapabilityOnEdgeAcs.java
/** * @param args/*w w w . j a v a 2s . c o m*/ */ public static void main(final String[] args) { HttpClient client = new HttpClient(); // client.getHostConfiguration().setProxy("", ); String host = null; String port = null; String address = "http://" + host + ":" + port + "/edge/api/"; String acsUsername = null; String acsPassword = null; if (host == null || port == null || acsUsername == null || acsPassword == null) { throw new InvalidParameterException("Fill host: " + host + ", port: " + port + ", acsUsername: " + acsUsername + ", and acsPassword: " + acsPassword + "."); } String realm = "NBBS_API_Realm"; AuthScope authscope = new AuthScope(host, Integer.parseInt(port), realm); // client.getState().setCredentials(realm, host, // new UsernamePasswordCredentials(acsUsername, acsPassword)); client.getState().setCredentials(authscope, new UsernamePasswordCredentials(acsUsername, acsPassword)); PostMethod post = null; // ----- // ----- Execution de la capability : changeDUStateUninstall // ----- post = new PostMethod(address + "capability/execute"); post.addParameter(new NameValuePair("deviceId", "10003")); post.addParameter(new NameValuePair("timeoutMs", "60000")); post.addParameter(new NameValuePair("capability", "\"changeDUStateUninstall\"")); // UUID: string // Version: string // ExecutionEnvRef: String JSONObject object = new JSONObject(); object.put("UUID", "45"); object.put("Version", "2.2.0"); object.put("ExecutionEnvRef", "ExecutionEnvRef_value"); post.addParameter(new NameValuePair("input", object.toString())); post.setDoAuthentication(true); // post.addParameter(new NameValuePair("deviceId", "60001")); // ----- // ----- Partie commune : Execution du post // ----- try { int status = client.executeMethod(post); System.out.println("status: " + status); String resp = post.getResponseBodyAsString(); System.out.println("resp: " + resp); } catch (HttpException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { // release any connection resources used by the method post.releaseConnection(); } }
From source file:BasicAuthenticationExample.java
public static void main(String[] args) throws Exception { HttpClient client = new HttpClient(); // pass our credentials to HttpClient, they will only be used for // authenticating to servers with realm "realm" on the host // "www.verisign.com", to authenticate against // an arbitrary realm or host change the appropriate argument to null. client.getState().setCredentials(new AuthScope("www.verisign.com", 443, "realm"), new UsernamePasswordCredentials("username", "password")); // 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("https://www.verisign.com/products/index.html"); // 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);/*from ww w.j a va 2 s. c o m*/ try { // execute the GET int status = client.executeMethod(get); // print the status and response System.out.println(status + "\n" + get.getResponseBodyAsString()); } finally { // release any connection resources used by the method get.releaseConnection(); } }
From source file:com.francetelecom.admindm.changedustate.callviaacs.CallChangeDUStateUpdateCapabilityOnEdgeAcs.java
/** * @param args/* ww w . ja va 2 s . c o m*/ */ public static void main(final String[] args) { HttpClient client = new HttpClient(); // client.getHostConfiguration().setProxy("", ); String host = null; String port = null; String address = "http://" + host + ":" + port + "/edge/api/"; String acsUsername = null; String acsPassword = null; if (host == null || port == null || acsUsername == null || acsPassword == null) { throw new InvalidParameterException("Fill host: " + host + ", port: " + port + ", acsUsername: " + acsUsername + ", and acsPassword: " + acsPassword + "."); } String realm = "NBBS_API_Realm"; AuthScope authscope = new AuthScope(host, Integer.parseInt(port), realm); // client.getState().setCredentials(realm, host, // new UsernamePasswordCredentials(acsUsername, acsPassword)); client.getState().setCredentials(authscope, new UsernamePasswordCredentials(acsUsername, acsPassword)); PostMethod post = null; // ----- // ----- Execution de la capability : changeDUStateUpdate // ----- post = new PostMethod(address + "capability/execute"); post.addParameter(new NameValuePair("deviceId", "10003")); post.addParameter(new NameValuePair("timeoutMs", "60000")); post.addParameter(new NameValuePair("capability", "\"changeDUStateUpdate\"")); // UUID: string // Version: string // URL: string // Username: string // Password: string JSONObject object = new JSONObject(); object.put("UUID", "45"); object.put("Version", "1.0.0"); object.put("URL", "http://127.0.0.1:8085/a/org.apache.felix.http.jetty-2.2.0.jar"); object.put("Username", "Username_value"); object.put("Password", "Password_value"); post.addParameter(new NameValuePair("input", object.toString())); post.setDoAuthentication(true); // post.addParameter(new NameValuePair("deviceId", "60001")); // ----- // ----- Partie commune : Execution du post // ----- try { int status = client.executeMethod(post); System.out.println("status: " + status); String resp = post.getResponseBodyAsString(); System.out.println("resp: " + resp); } catch (HttpException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { // release any connection resources used by the method post.releaseConnection(); } }
From source file:AlternateAuthenticationExample.java
public static void main(String[] args) throws Exception { HttpClient client = new HttpClient(); client.getState().setCredentials(new AuthScope("myhost", 80, "myrealm"), new UsernamePasswordCredentials("username", "password")); // Suppose the site supports several authetication schemes: NTLM and Basic // Basic authetication is considered inherently insecure. Hence, NTLM authentication // is used per default // This is to make HttpClient pick the Basic authentication scheme over NTLM & Digest List authPrefs = new ArrayList(3); authPrefs.add(AuthPolicy.BASIC);/*from w w w.j a v a 2 s . c o m*/ authPrefs.add(AuthPolicy.NTLM); authPrefs.add(AuthPolicy.DIGEST); client.getParams().setParameter(AuthPolicy.AUTH_SCHEME_PRIORITY, authPrefs); GetMethod httpget = new GetMethod("http://myhost/protected/auth-required.html"); try { int status = client.executeMethod(httpget); // print the status and response System.out.println(httpget.getStatusLine()); System.out.println(httpget.getResponseBodyAsString()); } finally { // release any connection resources used by the method httpget.releaseConnection(); } }