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); }
WebView webview = new WebView(); webview.setWebViewClient(new WebClient()); webView.getSettings().setJavaScriptEnabled(true); webview.loadUrl("stackoverflow.com");
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());
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(); }
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();
@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(); }
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()); }
WebClient client = new WebClient(); Page page = client.getPage("http://stackoverflow.com/users/flair/97901.json"); WebResponse response = page.getWebResponse(); if (response.getContentType().equals("application/json")) { String json = response.getContentAsString(); Map<String, String> map = new Gson().fromJson(json, new TypeToken<Map<String, String>>() {}.getType()); System.out.println(map.get("displayName")); // Benju }
URL url = new File("test.html").toURI().toURL(); WebClient webClient = new WebClient(); HtmlPage page = webClient.getPage(url); OutputStream os = null; try{ os = new FileOutputStream("test.pdf"); ITextRenderer renderer = new ITextRenderer(); renderer.setDocument(page,url.toString()); renderer.layout(); renderer.createPDF(os); } finally{ if(os != null) os.close(); }
public ActionResult GetPdf(string filename) { using (var client = new WebClient()) { var buffer = client.DownloadData("http://foo.com/bar.pdf"); return File(buffer, "application/pdf", "report1.pdf"); } }
using (WebClient client = new WebClient()) { string json = "{\"user\":{\"email\":\"sample@zmail\",\"password\":\"28902890\"}}"; client.UploadString("https://example.com/api/v1/sessions, json); }
using(WebClient client = new WebClient ()) { string url = "http://xxxxx.xxxxx.com/api/sendmsg.php?user=xxxx&pass=xxxx&sender=xxx&phone=xxxxxxxxxxx&text=this%20is%20test%20sms&priority=ndnd&stype=normal"; client.DownloadString(url); }
public void ProcessRequest(HttpContext context) { WebClient myClient = new WebClient(); //Fetch response on your server string response = myClient.DownloadString("http://example.com/webservice/webMethod"); // Send response to your javascript. context.Response.Write(response); }
button.Click += async delegate { var destination = Path.Combine( System.Environment.GetFolderPath( System.Environment.SpecialFolder.ApplicationData), "music.mp3"); await new WebClient().DownloadFileTaskAsync( new Uri("http://www.xyz.com/music.mp3"), destination); };
WebClient webClient = new WebClient(); webClient.setThrowExceptionOnFailingStatusCode(false); int status = webClient.getPage("http://google.co.uk/ffffff").getWebResponse() .getStatusCode(); System.out.println(status);
String html = "<html>Your html here</html>"; WebClient client = new WebClient(); MockWebConnection connection = new MockWebConnection(); connection.setDefaultResponse(html); client.setWebConnection(connection); HtmlPage page = client.getPage(someUrl);
final WebClient webClient = new WebClient(); final HtmlPage page1 = webClient.getPage("http://www.facebook.com"); final HtmlForm form = page1.getFormByName("login_form"); final HtmlSubmitInput button = form.getInputsByValue("Log in"); final HtmlTextInput textField = form.getInputByName("email"); textField.setValueAttribute("jon@jon.com"); final HtmlTextInput textField = form.getInputByName("pass"); textField.setValueAttribute("ahhhh"); final HtmlPage page2 = button.click();
public void submittingForm() throws Exception { WebClient webClient = new WebClient(); HtmlPage page1 = webClient.getPage("http://some_url"); HtmlForm form = page1.getFormByName("myform"); HtmlSubmitInput button = form.getInputByName("submitbutton"); HtmlTextInput textField = form.getInputByName("userid"); textField.setValueAttribute("root"); HtmlPage page2 = button.click(); }
final WebClient webClient = new WebClient(); final HtmlPage page1 = webClient.getPage("http://www.facebook.com"); final HtmlForm form = (HtmlForm) page1.getElementById("login_form"); final HtmlSubmitInput button = (HtmlSubmitInput) form.getInputsByValue("Log In").get(0); final HtmlTextInput textField = form.getInputByName("email"); textField.setValueAttribute("jon@jon.com"); final HtmlPasswordInput textField2 = form.getInputByName("pass"); textField2.setValueAttribute("ahhhh"); final HtmlPage page2 = button.click();