List of usage examples for org.apache.commons.httpclient HttpClient HttpClient
public HttpClient()
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 w w . j a va 2 s. co 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(); } }
From source file:com.zxm.servicemix.examples.cxf.jaxrs.client.Client.java
public static void main(String args[]) throws Exception { // Sent HTTP GET request to query all customer info // Sent HTTP GET request to query customer info System.out.println("Sent HTTP GET request to query customer info"); URL url = new URL("http://localhost:8181/cxf/crm/customerservice/customers/123"); InputStream in = url.openStream(); System.out.println(getStringFromInputStream(in)); // Sent HTTP GET request to query sub resource product info System.out.println("\n"); System.out.println("Sent HTTP GET request to query sub resource product info"); url = new URL("http://localhost:8181/cxf/crm/customerservice/orders/223/products/323"); in = url.openStream();//from www.j a v a2 s .com System.out.println(getStringFromInputStream(in)); // Sent HTTP PUT request to update customer info System.out.println("\n"); System.out.println("Sent HTTP PUT request to update customer info"); Client client = new Client(); String inputFile = client.getClass().getResource("update_customer.xml").getFile(); File input = new File(inputFile); PutMethod put = new PutMethod("http://localhost:8181/cxf/crm/customerservice/customers"); RequestEntity entity = new FileRequestEntity(input, "text/xml; charset=ISO-8859-1"); put.setRequestEntity(entity); HttpClient httpclient = new HttpClient(); try { int result = httpclient.executeMethod(put); System.out.println("Response status code: " + result); System.out.println("Response body: "); System.out.println(put.getResponseBodyAsString()); } finally { // Release current connection to the connection pool once you are // done put.releaseConnection(); } // Sent HTTP POST request to add customer System.out.println("\n"); System.out.println("Sent HTTP POST request to add customer"); inputFile = client.getClass().getResource("add_customer.xml").getFile(); input = new File(inputFile); PostMethod post = new PostMethod("http://localhost:8181/cxf/crm/customerservice/customers"); post.addRequestHeader("Accept", "text/xml"); entity = new FileRequestEntity(input, "text/xml; charset=ISO-8859-1"); post.setRequestEntity(entity); httpclient = new HttpClient(); try { int result = httpclient.executeMethod(post); System.out.println("Response status code: " + result); System.out.println("Response body: "); System.out.println(post.getResponseBodyAsString()); } finally { // Release current connection to the connection pool once you are // done post.releaseConnection(); } System.out.println("\n"); System.exit(0); }
From source file:com.mycompany.jaxrsexample.client.Client.java
public static void main(String args[]) throws Exception { // Sent HTTP GET request to query all students info System.out.println("Invoking server through HTTP GET to query all students info"); URL url = new URL("http://localhost:9000/studentservice/students"); InputStream in = url.openStream(); System.out.println(getStringFromInputStream(in)); // Sent HTTP GET request to query student info System.out.println("Sent HTTP GET request to query customer info"); url = new URL("http://localhost:9000/studentservice/students/1"); in = url.openStream();// ww w .j a v a2 s. co m System.out.println(getStringFromInputStream(in)); // Sent HTTP PUT request to update student info System.out.println("\n"); System.out.println("Sent HTTP PUT request to update student info"); Client client = new Client(); String inputFile = client.getClass().getResource("/update_student.xml").getFile(); URIResolver resolver = new URIResolver(inputFile); File input = new File(resolver.getURI()); PutMethod put = new PutMethod("http://localhost:9000/studentservice/students"); RequestEntity entity = new FileRequestEntity(input, "text/xml; charset=ISO-8859-1"); put.setRequestEntity(entity); HttpClient httpclient = new HttpClient(); try { int result = httpclient.executeMethod(put); System.out.println("Response status code: " + result); System.out.println("Response body: "); System.out.println(put.getResponseBodyAsString()); } finally { // Release current connection to the connection pool once you are // done put.releaseConnection(); } // Sent HTTP POST request to add student System.out.println("\n"); System.out.println("Sent HTTP POST request to add student"); inputFile = client.getClass().getResource("/add_student.xml").getFile(); resolver = new URIResolver(inputFile); input = new File(resolver.getURI()); PostMethod post = new PostMethod("http://localhost:9000/studentservice/students"); post.addRequestHeader("Accept", "text/xml"); entity = new FileRequestEntity(input, "text/xml; charset=ISO-8859-1"); post.setRequestEntity(entity); httpclient = new HttpClient(); try { int result = httpclient.executeMethod(post); System.out.println("Response status code: " + result); System.out.println("Response body: "); System.out.println(post.getResponseBodyAsString()); } finally { // Release current connection to the connection pool once you are // done post.releaseConnection(); } System.out.println("\n"); System.exit(0); }
From source file:edu.cmu.cs.diamond.pathfind.main.PathFindDjango.java
public static void main(String[] args) { if (args.length != 4 && args.length != 5) { System.out.println("usage: " + PathFindDjango.class.getName() + " predicate_dir interface_map slide_map annotation_uri"); return;// www. j a va 2 s .co m } final String predicateDir = args[0]; final String interfaceMap = args[1]; final String slideMap = args[2]; final String annotationUri = args[3]; final File slide; if (args.length == 5) { slide = new File(args[4]); } else { slide = null; } final AnnotationStore annotationStore = new DjangoAnnotationStore(new HttpClient(), annotationUri); SwingUtilities.invokeLater(new Runnable() { @Override public void run() { try { new PathFindFrame(predicateDir, annotationStore, interfaceMap, slideMap, slide, false); } catch (IOException e) { e.printStackTrace(); JOptionPane.showMessageDialog(null, e, "Error", JOptionPane.ERROR_MESSAGE); } } }); }
From source file:de.mpg.escidoc.services.cone.util.CCCrawler.java
public static void main(String[] args) throws Exception { HttpClient httpClient = new HttpClient(); querier = QuerierFactory.newQuerier(false); // field_commercial for (YesNo fieldCommercial : YesNo.values()) { // field_derivatives for (YesNo fieldDerivatives : YesNo.values()) { // field_derivatives for (Jurisdiction fieldJurisdiction : Jurisdiction.values()) { String licenceUrl = ccUrl + "&field_commercial=" + fieldCommercial.toString() + "&field_derivatives=" + fieldDerivatives.toString() + "&field_jurisdiction=" + fieldJurisdiction.toString() + "&lang=de_DE"; System.out.println(licenceUrl); GetMethod method = new GetMethod(licenceUrl); ProxyHelper.executeMethod(httpClient, method); if (method.getStatusCode() == 200) { TreeFragment fragment = new TreeFragment(); String key1 = "urn:cone:commercial"; String key3 = "urn:cone:jurisdiction"; List<LocalizedTripleObject> list = new ArrayList<LocalizedTripleObject>(); list.add(new LocalizedString(fieldCommercial.toBoolean())); fragment.put(key1, list); List<LocalizedTripleObject> list2 = new ArrayList<LocalizedTripleObject>(); list2.add(new LocalizedString(fieldDerivatives.toBoolean())); fragment.put("urn:cone:derivatives", list2); List<LocalizedTripleObject> list3 = new ArrayList<LocalizedTripleObject>(); list3.add(new LocalizedString(fieldJurisdiction.toString())); fragment.put(key3, list3); String codeToCopy = extractCode(method); Pattern urlPattern = Pattern.compile("href=\"([^\"]+)\""); Matcher urlMatcher = urlPattern.matcher(codeToCopy); if (urlMatcher.find()) { String url = urlMatcher.group(1); fragment.setSubject(url); Pattern versionPattern = Pattern.compile("/(\\d+\\.\\d+)/[^/]+/$"); Matcher versionMatcher = versionPattern.matcher(url); if (versionMatcher.find()) { list = new ArrayList<LocalizedTripleObject>(); list.add(new LocalizedString(versionMatcher.group(1))); fragment.put("urn:cone:version", list); }//from w w w . j av a2 s .co m Pattern imgPattern = Pattern.compile("src=\"([^\"]+)\""); Matcher imgMatcher = imgPattern.matcher(codeToCopy); if (imgMatcher.find()) { list = new ArrayList<LocalizedTripleObject>(); list.add(new LocalizedString(imgMatcher.group(1))); fragment.put("http://xmlns.com/foaf/0.1/depiction", list); } GetMethod method2 = new GetMethod(url); ProxyHelper.executeMethod(httpClient, method2); String page = method2.getResponseBodyAsString(); Pattern namePattern = Pattern.compile("<h2 property=\"dc:title\">([^<]+)</h2>"); Matcher nameMatcher = namePattern.matcher(page); if (nameMatcher.find()) { list = new ArrayList<LocalizedTripleObject>(); list.add(new LocalizedString(nameMatcher.group(1))); fragment.put("http://purl.org/dc/elements/1.1/title", list); } List<LocalizedTripleObject> languages = extractLanguages(page, url); fragment.put("urn:cone:translation", languages); querier.delete("cclicences", url); querier.create("cclicences", url, fragment); } } else { System.out.println("Not found: " + licenceUrl); } } } } querier.release(); }
From source file:CookieDemoApp.java
/** * * Usage://from ww w. j ava 2s. co m * java CookieDemoApp http://mywebserver:80/ * * @param args command line arguments * Argument 0 is a URL to a web server * * */ public static void main(String[] args) throws Exception { if (args.length != 1) { System.err.println("Usage: java CookieDemoApp <url>"); System.err.println("<url> The url of a webpage"); System.exit(1); } // Get target URL String strURL = args[0]; System.out.println("Target URL: " + strURL); // Get initial state object HttpState initialState = new HttpState(); // Initial set of cookies can be retrieved from persistent storage and // re-created, using a persistence mechanism of choice, Cookie mycookie = new Cookie(".foobar.com", "mycookie", "stuff", "/", null, false); // and then added to your HTTP state instance initialState.addCookie(mycookie); // Get HTTP client instance HttpClient httpclient = new HttpClient(); httpclient.getHttpConnectionManager().getParams().setConnectionTimeout(30000); httpclient.setState(initialState); // RFC 2101 cookie management spec is used per default // to parse, validate, format & match cookies httpclient.getParams().setCookiePolicy(CookiePolicy.RFC_2109); // A different cookie management spec can be selected // when desired //httpclient.getParams().setCookiePolicy(CookiePolicy.NETSCAPE); // Netscape Cookie Draft spec is provided for completeness // You would hardly want to use this spec in real life situations //httppclient.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY); // Compatibility policy is provided in order to mimic cookie // management of popular web browsers that is in some areas // not 100% standards compliant // Get HTTP GET method GetMethod httpget = new GetMethod(strURL); // Execute HTTP GET int result = httpclient.executeMethod(httpget); // Display status code System.out.println("Response status code: " + result); // Get all the cookies Cookie[] cookies = httpclient.getState().getCookies(); // Display the cookies System.out.println("Present cookies: "); for (int i = 0; i < cookies.length; i++) { System.out.println(" - " + cookies[i].toExternalForm()); } // Release current connection to the connection pool once you are done httpget.releaseConnection(); }
From source file:edu.techseekers.training.client.Client.java
public static void main(String args[]) throws Exception { // Sent HTTP GET request to query all customer info /*/*from w ww . j a v a 2 s. c om*/ * URL url = new URL("http://localhost:9000/customers"); * System.out.println("Invoking server through HTTP GET to query all * customer info"); InputStream in = url.openStream(); StreamSource * source = new StreamSource(in); printSource(source); */ // Sent HTTP GET request to query customer info System.out.println("Sent HTTP GET request to query customer info"); URL url = new URL("http://localhost:9000/customerservice/customers/123"); InputStream in = url.openStream(); System.out.println(getStringFromInputStream(in)); // Sent HTTP GET request to query sub resource product info System.out.println("\n"); System.out.println("Sent HTTP GET request to query sub resource product info"); url = new URL("http://localhost:9000/customerservice/orders/223/products/323"); in = url.openStream(); System.out.println(getStringFromInputStream(in)); // Sent HTTP PUT request to update customer info System.out.println("\n"); System.out.println("Sent HTTP PUT request to update customer info"); Client client = new Client(); String inputFile = client.getClass().getResource("/update_customer.xml").getFile(); URIResolver resolver = new URIResolver(inputFile); File input = new File(resolver.getURI()); PutMethod put = new PutMethod("http://localhost:9000/customerservice/customers"); RequestEntity entity = new FileRequestEntity(input, "text/xml; charset=ISO-8859-1"); put.setRequestEntity(entity); HttpClient httpclient = new HttpClient(); try { int result = httpclient.executeMethod(put); System.out.println("Response status code: " + result); System.out.println("Response body: "); System.out.println(put.getResponseBodyAsString()); } finally { // Release current connection to the connection pool once you are // done put.releaseConnection(); } // Sent HTTP POST request to add customer System.out.println("\n"); System.out.println("Sent HTTP POST request to add customer"); inputFile = client.getClass().getResource("/add_customer.xml").getFile(); resolver = new URIResolver(inputFile); input = new File(resolver.getURI()); PostMethod post = new PostMethod("http://localhost:9000/customerservice/customers"); post.addRequestHeader("Accept", "text/xml"); entity = new FileRequestEntity(input, "text/xml; charset=ISO-8859-1"); post.setRequestEntity(entity); httpclient = new HttpClient(); try { int result = httpclient.executeMethod(post); System.out.println("Response status code: " + result); System.out.println("Response body: "); System.out.println(post.getResponseBodyAsString()); } finally { // Release current connection to the connection pool once you are // done post.releaseConnection(); } System.out.println("\n"); System.exit(0); }
From source file:main.java.demo.jaxrs.client.Client.java
public static void main(String args[]) throws Exception { // Sent HTTP GET request to query all customer info /*/*from w w w .j a va2s . c o m*/ * URL url = new URL("http://localhost:9000/customers"); * System.out.println("Invoking server through HTTP GET to query all * customer info"); InputStream in = url.openStream(); StreamSource * source = new StreamSource(in); printSource(source); */ // Sent HTTP GET request to query customer info System.out.println("Sent HTTP GET request to query customer info"); URL url = new URL("http://localhost:8989/RestFull/service1/customerservice/customers/123"); InputStream in = url.openStream(); System.out.println(getStringFromInputStream(in)); // Sent HTTP GET request to query sub resource product info System.out.println("\n"); System.out.println("Sent HTTP GET request to query sub resource product info"); url = new URL("http://localhost:9000/customerservice/orders/223/products/323"); in = url.openStream(); System.out.println(getStringFromInputStream(in)); // Sent HTTP PUT request to update customer info System.out.println("\n"); System.out.println("Sent HTTP PUT request to update customer info"); Client client = new Client(); String inputFile = client.getClass().getResource("update_customer.xml").getFile(); URIResolver resolver = new URIResolver(inputFile); File input = new File(resolver.getURI()); PutMethod put = new PutMethod("http://localhost:9000/customerservice/customers"); RequestEntity entity = new FileRequestEntity(input, "text/xml; charset=ISO-8859-1"); put.setRequestEntity(entity); HttpClient httpclient = new HttpClient(); try { int result = httpclient.executeMethod(put); System.out.println("Response status code: " + result); System.out.println("Response body: "); System.out.println(put.getResponseBodyAsString()); } finally { // Release current connection to the connection pool once you are // done put.releaseConnection(); } // Sent HTTP POST request to add customer System.out.println("\n"); System.out.println("Sent HTTP POST request to add customer"); inputFile = client.getClass().getResource("add_customer.xml").getFile(); resolver = new URIResolver(inputFile); input = new File(resolver.getURI()); PostMethod post = new PostMethod("http://localhost:9000/customerservice/customers"); post.addRequestHeader("Accept", "text/xml"); entity = new FileRequestEntity(input, "text/xml; charset=ISO-8859-1"); post.setRequestEntity(entity); httpclient = new HttpClient(); try { int result = httpclient.executeMethod(post); System.out.println("Response status code: " + result); System.out.println("Response body: "); System.out.println(post.getResponseBodyAsString()); } finally { // Release current connection to the connection pool once you are // done post.releaseConnection(); } System.out.println("\n"); System.exit(0); }
From source file:main.java.client.Client.java
public static void main(String args[]) throws Exception { // Sent HTTP GET request to query all customer info /*/*from w w w . j ava 2 s .c o m*/ * URL url = new URL("http://localhost:9000/customers"); * System.out.println("Invoking server through HTTP GET to query all * customer info"); InputStream in = url.openStream(); StreamSource * source = new StreamSource(in); printSource(source); */ // Sent HTTP GET request to query customer info System.out.println("Sent HTTP GET request to query customer info"); URL url = new URL("http://localhost:8080/authenticate/customers/123"); InputStream in = url.openStream(); System.out.println(getStringFromInputStream(in)); // Sent HTTP GET request to query sub resource product info System.out.println("\n"); System.out.println("Sent HTTP GET request to query sub resource product info"); url = new URL("http://localhost:9000/authenticate/orders/223/products/323"); in = url.openStream(); System.out.println(getStringFromInputStream(in)); // Sent HTTP PUT request to update customer info System.out.println("\n"); System.out.println("Sent HTTP PUT request to update customer info"); Client client = new Client(); String inputFile = client.getClass().getResource("/update_customer.xml").getFile(); URIResolver resolver = new URIResolver(inputFile); File input = new File(resolver.getURI()); PutMethod put = new PutMethod("http://localhost:9000/authenticate/customers"); RequestEntity entity = new FileRequestEntity(input, "text/xml; charset=ISO-8859-1"); put.setRequestEntity(entity); HttpClient httpclient = new HttpClient(); try { int result = httpclient.executeMethod(put); System.out.println("Response status code: " + result); System.out.println("Response body: "); System.out.println(put.getResponseBodyAsString()); } finally { // Release current connection to the connection pool once you are // done put.releaseConnection(); } // Sent HTTP POST request to add customer System.out.println("\n"); System.out.println("Sent HTTP POST request to add customer"); inputFile = client.getClass().getResource("/add_customer.xml").getFile(); resolver = new URIResolver(inputFile); input = new File(resolver.getURI()); PostMethod post = new PostMethod("http://localhost:9000/authenticate/customers"); post.addRequestHeader("Accept", "text/xml"); entity = new FileRequestEntity(input, "text/xml; charset=ISO-8859-1"); post.setRequestEntity(entity); httpclient = new HttpClient(); try { int result = httpclient.executeMethod(post); System.out.println("Response status code: " + result); System.out.println("Response body: "); System.out.println(post.getResponseBodyAsString()); } finally { // Release current connection to the connection pool once you are // done post.releaseConnection(); } // Sent HTTP POST request to add json customer System.out.println("\n"); System.out.println("Sent HTTP POST request to add json customer"); String inputFilej = client.getClass().getResource("/add_customer.json").getFile(); URIResolver resolverj = new URIResolver(inputFilej); File inputj = new File(resolverj.getURI()); PostMethod postj = new PostMethod("http://localhost:9000/authenticate/customersjson"); postj.addRequestHeader("Accept", "application/json"); RequestEntity entityj = new FileRequestEntity(inputj, "application/json; charset=ISO-8859-1"); postj.setRequestEntity(entityj); HttpClient httpclientj = new HttpClient(); try { int resultj = httpclientj.executeMethod(postj); System.out.println("Response status code json: " + resultj); System.out.println("Response body json: "); System.out.println(postj.getResponseBodyAsString()); } finally { // Release current connection to the connection pool once you are // done postj.releaseConnection(); } System.out.println("\n"); System.exit(0); }
From source file:TrivialApp.java
public static void main(String[] args) { if ((args.length != 1) && (args.length != 3)) { printUsage();/*from ww w .j av a 2 s. co m*/ 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); }