public async void HandleButtonClick(object sender, EventArgs e) { // All of this method will run in the UI thread, which it needs // to as it touches the UI... however, it won't block when it does // the web operation. string url = urlTextBox.Text; WebClient client = new WebClient(); string webText = await client.DownloadStringTaskAsync(url); // Continuation... automatically called in the UI thread, with appropriate // context (local variables etc) which we used earlier. sizeTextBox.Text = string.Format("{0}: {1}", url, webText.Length); }
WebClient client = new WebClient(); client.getOptions().setThrowExceptionOnScriptError(false);
@Grapes( @Grab(group='net.sourceforge.htmlunit', module='htmlunit', version='2.8') ) import com.gargoylesoftware.htmlunit.* import com.gargoylesoftware.htmlunit.html.* URL url = new URL("http://www.example.com"); StringWebResponse response = new StringWebResponse("<html><head><title>Test</title></head><body></body></html>", url); WebClient client = new WebClient() HtmlPage page = HTMLParser.parseHtml(response, client.getCurrentWindow()); System.out.println(page.getTitleText());
@Test public void testFloatInHeader() { Response response = WebClient.create(endPoint + TIKA_PATH) .type("application/pdf") .accept("text/plain") .header(TikaResource.X_TIKA_PDF_HEADER_PREFIX + "averageCharTolerance", "2.0") .put(ClassLoader.getSystemResourceAsStream("testOCR.pdf")); assertEquals(200, response.getStatus()); }
protected static boolean canRun() { Response response = null; try { response = WebClient.create(readRestUrl() + GROBID_ISALIVE_PATH) .accept(MediaType.TEXT_HTML).get(); String resp = response.readEntity(String.class); return resp != null && !resp.equals("") && resp.startsWith("<h4>"); } catch (Exception e) { //swallow...can't run return false; } }
WebClient client = new WebClient(); HtmlPage page = client.getPage("http://stackoverflow.com"); // create a submit button - it doesn't work with 'input' HtmlElement button = page.createElement("button"); button.setAttribute("type", "submit"); // append the button to the form HtmlElement form = ...; form.appendChild(button); // submit the form page = button.click();
public static void main(String[] args) throws Exception { LogFactory.getFactory().setAttribute("org.apache.commons.logging.Log", "org.apache.commons.logging.impl.NoOpLog"); File file = new File("cookie.file"); ObjectInputStream in = new ObjectInputStream(new FileInputStream(file)); Set<Cookie> cookies = (Set<Cookie>) in.readObject(); in.close(); WebClient wc = new WebClient(); Iterator<Cookie> i = cookies.iterator(); while (i.hasNext()) { wc.getCookieManager().addCookie(i.next()); } HtmlPage p = wc.getPage("http://google.com"); ObjectOutput out = new ObjectOutputStream(new FileOutputStream("cookie.file")); out.writeObject(wc.getCookieManager().getCookies()); out.close(); }
@Test public void submittingForm() throws Exception { final WebClient webClient = new WebClient(); // Get the first page final HtmlPage page1 = webClient.getPage("http://some_url"); // Get the form that we are dealing with and within that form, // find the submit button and the field that we want to change. final HtmlForm form = page1.getFormByName("myform"); final HtmlSubmitInput button = form.getInputByName("submitbutton"); final HtmlTextInput textField = form.getInputByName("userid"); // Change the value of the text field textField.setValueAttribute("root"); // Now submit the form by clicking the button and get back the second page. final HtmlPage page2 = button.click(); webClient.closeAllWindows(); }
WebView webview = new WebView(); webview.setWebViewClient(new WebClient()); webView.getSettings().setJavaScriptEnabled(true); webview.loadUrl("stackoverflow.com");
@Test public void testTrustedMethodPrevention() { Response response = WebClient.create(endPoint + TIKA_PATH) .type("application/pdf") .accept("text/plain") .header(TikaResource.X_TIKA_OCR_HEADER_PREFIX + "trustedPageSeparator", "\u0020") .put(ClassLoader.getSystemResourceAsStream("testOCR.pdf")); assertEquals(500, response.getStatus()); }
@Test public void testProperPathWelcome() throws Exception{ Response response = WebClient .create(endPoint + WELCOME_PATH) .type("text/html") .accept("text/html") .get(); String html = getStringFromInputStream((InputStream) response.getEntity()); assertContains(PATH_RESOURCE, html); assertContains(PATH_RESOURCE_2, html); } }
public static void main(String[] args) throws Exception { // turn off htmlunit warnings java.util.logging.Logger.getLogger("com.gargoylesoftware.htmlunit").setLevel(java.util.logging.Level.OFF); java.util.logging.Logger.getLogger("org.apache.http").setLevel(java.util.logging.Level.OFF); WebClient webClient = new WebClient(); HtmlPage page = webClient.getPage("http://stackoverflow.com"); System.out.println(page.getTitleText()); }
@Test public void testGetPlainText() throws Exception { Response response = WebClient .create(endPoint + MIMETYPES_PATH) .type("text/plain") .accept("text/plain") .get(); String text = getStringFromInputStream((InputStream) response.getEntity()); assertContains("text/plain", text); assertContains("application/xml", text); assertContains("video/x-ogm", text); assertContains("supertype: video/ogg", text); assertContains("alias: image/x-ms-bmp", text); }
@Test public void testDetectEnglishString() throws Exception { String url = endPoint + LANG_STRING_PATH; Response response = WebClient.create(url).type("text/plain") .accept("text/plain").put(ENGLISH_STRING); assertNotNull(response); String readLang = getStringFromInputStream((InputStream) response .getEntity()); assertEquals("en", readLang); }
@Test public void testDetectFrenchString() throws Exception { String url = endPoint + LANG_STRING_PATH; Response response = WebClient.create(url).type("text/plain") .accept("text/plain").put(FRENCH_STRING); assertNotNull(response); String readLang = getStringFromInputStream((InputStream) response .getEntity()); assertEquals("fr", readLang); }
@Test public void testTranslateFull() throws Exception { String url = endPoint + TRANSLATE_ALL_PATH + LINGO_PATH + SRCDEST; Response response = WebClient.create(url).type("text/plain") .accept("*/*").put(TRANSLATE_TXT); assertNotNull(response); String translated = getStringFromInputStream((InputStream) response .getEntity()); assertEquals(TRANSLATE_TXT, translated); }