/** * Load the supported languages from the <a href="https://developer.lingo24.com/premium-machine-translation-api">Premium MT API</a>. * Support is continually expanding. * @return <code>Set<String></code> of supported languages. */ private Set<String> getAllLanguages() { Set<String> languages = new HashSet<>(); if (!isAvailable) { return languages; } WebClient _client = null; try { _client = WebClient.create(LINGO24_TRANSLATE_URL_BASE + LINGO24_SOURCELANG_ACTION); Response response = _client.accept(MediaType.APPLICATION_JSON) .query("user_key", userKey).get(); String json = response.readEntity(String.class); JsonArray jsonArray = new JsonParser().parse(json).getAsJsonObject().get("source_langs").getAsJsonArray(); for (JsonElement jsonElement : jsonArray) { languages.add(jsonElement.getAsJsonArray().get(0).getAsString()); } } catch (Throwable e) { LOG.warn("problem detecting", e); } finally { if (_client != null) { _client.close(); } } return languages; }
@Override public void close() { if (!closed) { synchronized (baseClients) { for (WebClient wc : baseClients) { wc.close(); } } baseClients = null; closed = true; } }
@Override public void close() { if (!closed) { synchronized (baseClients) { for (WebClient wc : baseClients) { wc.close(); } } baseClients = null; closed = true; } }
// load page using HTML Unit and fire scripts WebClient webClient = new WebClient(); HtmlPage myPage = webClient.getPage(myURL); // convert page to generated HTML and convert to document doc = Jsoup.parse(myPage.asXml()); // do something with html content System.out.println(doc.html()); // clean up resources webClient.close();
// capture rendered page WebClient webClient = new WebClient(); HtmlPage myPage = webClient.getPage("https://pokevision.com"); // convert to jsoup dom Document doc = Jsoup.parse(myPage.asXml()); // extract data using jsoup selectors Elements images = doc.select("img[src~=(?i)\\.(png|jpe?g|gif)]"); for (Element image : images) { System.out.println("src : " + image.attr("src")); } // clean up resources webClient.close();
// load page using HTML Unit and fire scripts WebClient webClient = new WebClient(); HtmlPage myPage = webClient.getPage(myURL); // convert page to generated HTML and convert to document Document doc = Jsoup.parse(myPage.asXml(), baseURI); // clean up resources webClient.close();
WebClient webClient = new WebClient(BrowserVersion.CHROME); String url = "https://github.com/login"; webClient.getOptions().setJavaScriptEnabled(true); webClient.getOptions().setThrowExceptionOnScriptError(false); webClient.getOptions().setThrowExceptionOnFailingStatusCode(false); HtmlPage page = webClient.getPage(url); DomElement form = (DomElement) page.querySelector("form"); System.out.println(form.asXml()); webClient.close();
try(final WebClient webClient = new WebClient(BrowserVersion.CHROME)) { java.util.logging.Logger.getLogger("com.gargoylesoftware.htmlunit").setLevel(Level.OFF); webClient.setAjaxController(new NicelyResynchronizingAjaxController()); webClient.getOptions().setThrowExceptionOnScriptError(false); webClient.getOptions().setUseInsecureSSL(true); webClient.waitForBackgroundJavaScript(5 * 1000); HtmlPage page = webClient.getPage("https://jumpseller.cl/support/webpayplus/"); String stringToSave = page.asXml(); // It's a string with full HTML-code, if need you can save it to file. webClient.close(); }
WebClient client = new WebClient(BrowserVersion.CHROME); client.getOptions().setJavaScriptEnabled(true); client.getOptions().setThrowExceptionOnScriptError(false); client.getOptions().setThrowExceptionOnFailingStatusCode(false); HtmlPage page = client.getPage("http://yoursite.com/"); HtmlTextInput user = page.getElementByName("USER_ID"); user.setValueAttribute("youruser"); HtmlPasswordInput password = page.getElementByName("PWD"); password.setValueAttribute("yourpassword"); HtmlButtonInput loginButton = (HtmlButtonInput) page.getByXPath("//input[@class='loginbtn']").get(0); page = loginButton.click(); System.out.println(page.asText()); client.close();
@Test public void testGetBookAsyncResponse404() throws Exception { String address = "http://localhost:" + PORT + "/bookstore/bookheaders/404"; WebClient wc = createWebClient(address); Future<Response> future = wc.async().get(Response.class); assertEquals(404, future.get().getStatus()); wc.close(); }
@Test public void testGetBookResponseProcessingException() throws Exception { String address = "http://localhost:" + PORT + "/bookstore/books/123"; List<Object> providers = new ArrayList<>(); providers.add(new FaultyBookReader()); WebClient wc = WebClient.create(address, providers); Future<Book> future = wc.async().get(Book.class); try { future.get(); fail("Exception expected"); } catch (ExecutionException ex) { assertTrue(ex.getCause() instanceof ResponseProcessingException); } wc.close(); }
@Test public void testNonExistent() throws Exception { String address = "http://168.168.168.168/bookstore"; List<Object> providers = new ArrayList<>(); providers.add(new TestResponseFilter()); WebClient wc = WebClient.create(address, providers); Future<Book> future = wc.async().get(Book.class); try { future.get(); fail("Exception expected"); } catch (ExecutionException ex) { Throwable cause = ex.getCause(); assertTrue(cause instanceof ProcessingException); assertTrue(ex.getCause().getCause() instanceof IOException); } finally { wc.close(); } } @Test
@Test public void testGetBookAsync404() throws Exception { String address = "http://localhost:" + PORT + "/bookstore/bookheaders/404"; WebClient wc = createWebClient(address); Future<Book> future = wc.async().get(Book.class); try { future.get(); fail("Exception expected"); } catch (ExecutionException ex) { assertTrue(ex.getCause() instanceof NotFoundException); } wc.close(); }
@Test public void testPatchBookInputStream() throws Exception { String address = "http://localhost:" + PORT + "/bookstore/patch"; WebClient wc = WebClient.create(address); wc.type("application/xml"); WebClient.getConfig(wc).getRequestContext().put("use.async.http.conduit", true); Book book = wc.invoke("PATCH", new ByteArrayInputStream( "<Book><name>Patch</name><id>123</id></Book>".getBytes()), Book.class); assertEquals("Patch", book.getName()); wc.close(); }
@Test public void testPostBookProcessingException() throws Exception { String address = "http://localhost:" + PORT + "/bookstore/"; List<Object> providers = new ArrayList<>(); providers.add(new FaultyBookWriter()); WebClient wc = WebClient.create(address, providers); Future<Book> future = wc.async().post(Entity.xml(new Book()), Book.class); try { future.get(); fail("Exception expected"); } catch (ExecutionException ex) { assertTrue(ex.getCause() instanceof ProcessingException); } wc.close(); }
@Test public void testGetBookAsync404Callback() throws Exception { String address = "http://localhost:" + PORT + "/bookstore/bookheaders/404"; WebClient wc = createWebClient(address); final Holder<Object> holder = new Holder<>(); InvocationCallback<Object> callback = createCallback(holder); try { wc.async().get(callback).get(); fail("Exception expected"); } catch (ExecutionException ex) { assertTrue(ex.getCause() instanceof NotFoundException); assertTrue(ex.getCause() == holder.value); } wc.close(); }
@Test public void testPatchBook() throws Exception { String address = "http://localhost:" + PORT + "/bookstore/patch"; WebClient wc = WebClient.create(address); wc.type("application/xml"); WebClient.getConfig(wc).getRequestContext().put("use.async.http.conduit", true); Book book = wc.invoke("PATCH", new Book("Patch", 123L), Book.class); assertEquals("Patch", book.getName()); wc.close(); }
@Test public void testRetrieveBookCustomMethodAsync() throws Exception { String address = "http://localhost:" + PORT + "/bookstore/retrieve"; WebClient wc = WebClient.create(address); wc.accept("application/xml"); Future<Book> book = wc.async().method("RETRIEVE", Entity.xml(new Book("Retrieve", 123L)), Book.class); assertEquals("Retrieve", book.get().getName()); wc.close(); }
@Test public void testDeleteWithBody() throws Exception { String address = "http://localhost:" + PORT + "/bookstore/deletebody"; WebClient wc = WebClient.create(address); wc.type("application/xml").accept("application/xml"); WebClient.getConfig(wc).getRequestContext().put("use.async.http.conduit", true); Book book = wc.invoke("DELETE", new Book("Delete", 123L), Book.class); assertEquals("Delete", book.getName()); wc.close(); }
@Test public void testRetrieveBookCustomMethodAsyncSync() throws Exception { String address = "http://localhost:" + PORT + "/bookstore/retrieve"; WebClient wc = WebClient.create(address); // Setting this property is not needed given that // we have the async conduit loaded in the test module: // WebClient.getConfig(wc).getRequestContext().put("use.async.http.conduit", true); wc.type("application/xml").accept("application/xml"); Book book = wc.invoke("RETRIEVE", new Book("Retrieve", 123L), Book.class); assertEquals("Retrieve", book.getName()); wc.close(); }