List of usage examples for org.apache.commons.httpclient HttpClient getState
public HttpState getState()
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);/*from w ww .ja v a2s .c o m*/ String response = method.getResponseBodyAsString(); System.out.println(response); method.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);// w ww. ja va2s . c om 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(); } }
From source file:com.discursive.jccook.httpclient.NTLMAuthExample.java
public static void main(String[] args) throws HttpException, IOException { HttpClient client = new HttpClient(); // Set credentials on the client Credentials credentials = new NTCredentials("testuser", "crazypass", "tobrien.discursive.com", "DOMAIN"); client.getState().setCredentials(null, null, credentials); String url = "http://webmail.domain.biz/exchange/"; HttpMethod method = new GetMethod(url); client.executeMethod(method);//from w w w . j ava 2 s . c o m String response = method.getResponseBodyAsString(); System.out.println(response); method.releaseConnection(); }
From source file:GetCookiePrintAndSetValue.java
public static void main(String args[]) throws Exception { HttpClient client = new HttpClient(); client.getParams().setParameter("http.useragent", "My Browser"); GetMethod method = new GetMethod("http://localhost:8080/"); try {/*from w ww . j a v a2 s .c om*/ client.executeMethod(method); Cookie[] cookies = client.getState().getCookies(); for (int i = 0; i < cookies.length; i++) { Cookie cookie = cookies[i]; System.err.println("Cookie: " + cookie.getName() + ", Value: " + cookie.getValue() + ", IsPersistent?: " + cookie.isPersistent() + ", Expiry Date: " + cookie.getExpiryDate() + ", Comment: " + cookie.getComment()); cookie.setValue("My own value"); } client.executeMethod(method); } catch (Exception e) { System.err.println(e); } finally { method.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);/*ww w .j a v a 2 s .c om*/ 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:TrivialApp.java
public static void main(String[] args) { if ((args.length != 1) && (args.length != 3)) { printUsage();//from ww w . j a v a 2s . c om System.exit(-1); } Credentials creds = null; if (args.length >= 3) { creds = new UsernamePasswordCredentials(args[1], args[2]); } //create a singular HttpClient object HttpClient client = new HttpClient(); //establish a connection within 5 seconds client.getHttpConnectionManager().getParams().setConnectionTimeout(5000); //set the default credentials if (creds != null) { client.getState().setCredentials(AuthScope.ANY, creds); } String url = args[0]; HttpMethod method = null; //create a method object method = new GetMethod(url); method.setFollowRedirects(true); //} catch (MalformedURLException murle) { // System.out.println("<url> argument '" + url // + "' is not a valid URL"); // System.exit(-2); //} //execute the method String responseBody = null; try { client.executeMethod(method); responseBody = method.getResponseBodyAsString(); } catch (HttpException he) { System.err.println("Http error connecting to '" + url + "'"); System.err.println(he.getMessage()); System.exit(-4); } catch (IOException ioe) { System.err.println("Unable to connect to '" + url + "'"); System.exit(-3); } //write out the request headers System.out.println("*** Request ***"); System.out.println("Request Path: " + method.getPath()); System.out.println("Request Query: " + method.getQueryString()); Header[] requestHeaders = method.getRequestHeaders(); for (int i = 0; i < requestHeaders.length; i++) { System.out.print(requestHeaders[i]); } //write out the response headers System.out.println("*** Response ***"); System.out.println("Status Line: " + method.getStatusLine()); Header[] responseHeaders = method.getResponseHeaders(); for (int i = 0; i < responseHeaders.length; i++) { System.out.print(responseHeaders[i]); } //write out the response body System.out.println("*** Response Body ***"); System.out.println(responseBody); //clean up the connection resources method.releaseConnection(); System.exit(0); }
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 {/*from w w w.j a v a 2s . c om*/ 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);/*from w w w.j a v a2 s . com*/ 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.francetelecom.admindm.changedustate.callviaacs.CallChangeDUStateUninstallCapabilityOnEdgeAcs.java
/** * @param args/*from ww w . jav a 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 : 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:com.francetelecom.admindm.changedustate.callviaacs.CallChangeDUStateUpdateCapabilityOnEdgeAcs.java
/** * @param args/*from w ww . j a v a 2s . com*/ */ 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(); } }