@Before public void setup() throws Exception { webRequest = new WebRequest(new URL("http://example.com:80/test/this/here")); webRequest.setHttpMethod(HttpMethod.GET); requestBuilder = new HtmlUnitRequestBuilder(sessions, webClient, webRequest); }
@Test @SuppressWarnings("deprecation") public void buildRequestInputStream() throws Exception { String content = "some content that has length"; webRequest.setHttpMethod(HttpMethod.POST); webRequest.setRequestBody(content); MockHttpServletRequest actualRequest = requestBuilder.buildRequest(servletContext); assertThat(IOUtils.toString(actualRequest.getInputStream()), equalTo(content)); }
@Test public void buildRequestReader() throws Exception { String expectedBody = "request body"; webRequest.setHttpMethod(HttpMethod.POST); webRequest.setRequestBody(expectedBody); MockHttpServletRequest actualRequest = requestBuilder.buildRequest(servletContext); assertThat(IOUtils.toString(actualRequest.getReader()), equalTo(expectedBody)); }
@Test public void buildRequestContentLength() { String content = "some content that has length"; webRequest.setHttpMethod(HttpMethod.POST); webRequest.setRequestBody(content); MockHttpServletRequest actualRequest = requestBuilder.buildRequest(servletContext); assertThat(actualRequest.getContentLength(), equalTo(content.length())); }
@Test public void buildRequestMethods() { for (HttpMethod expectedMethod : HttpMethod.values()) { webRequest.setHttpMethod(expectedMethod); String actualMethod = requestBuilder.buildRequest(servletContext).getMethod(); assertThat(actualMethod, equalTo(expectedMethod.name())); } }
/** * Instantiates a {@link WebRequest} for the specified URL using the specified HTTP submit method. * @param url the target URL * @param submitMethod the HTTP submit method to use */ public WebRequest(final URL url, final HttpMethod submitMethod) { this(url); setHttpMethod(submitMethod); }
/** * Instantiates a {@link WebRequest} for the specified URL using the specified HTTP submit method. * @param url the target URL * @param submitMethod the HTTP submit method to use */ public WebRequest(final URL url, final HttpMethod submitMethod) { this(url); setHttpMethod(submitMethod); }
String url = "http://cpdocket.cp.cuyahogacounty.us/Search.aspx"; String EventTarget = "ctl00$SheetContentPlaceHolder$rbCivilForeclosure"; String world = "ctl00$SheetContentPlaceHolder$UpdatePanel1|ctl00$SheetContentPlaceHolder$rbCivilForeclosure"; String Viewstate = page2.getElementById("__VIEWSTATE").getAttribute("value"); String EventValidation = page2.getElementById("__EVENTVALIDATION").getAttribute("value"); WebRequest req1 = new WebRequest(new URL(url)); req1.setHttpMethod(HttpMethod.POST); req1.setAdditionalHeader("Origin", "http://cpdocket.cp.cuyahogacounty.us"); req1.setAdditionalHeader("Referer", "http://cpdocket.cp.cuyahogacounty.us/Search.aspx"); req1.setAdditionalHeader("X-Requested-With", "XMLHttpRequest"); String txtview1 = "ctl00$ScriptManager1=" + URLEncoder.encode(world) + "&__EVENTTARGET=" + URLEncoder.encode(EventTarget) + "&__EVENTARGUMENT=&__LASTFOCUS=&__VIEWSTATE=" + URLEncoder.encode(Viewstate) + "&__EVENTVALIDATION=" + URLEncoder.encode(EventValidation) + "&ctl00$SheetContentPlaceHolder$rbSearches=forcl&__ASYNCPOST=true&"; //System.out.println("this is text view =============== " + txtview1); req1.setRequestBody(txtview1); req1.setAdditionalHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8"); String re=client.getPage(req1).getWebResponse().getContentAsString(); System.out.println("========== " + re);
/** * Prepares the WebRequest that will be sent. * @param content the content to send */ private void prepareRequest(final Object content) { if (content != null && content != Undefined.instance) { if (!"".equals(content) && HttpMethod.GET == webRequest_.getHttpMethod()) { webRequest_.setHttpMethod(HttpMethod.POST); } if (HttpMethod.POST == webRequest_.getHttpMethod() || HttpMethod.PUT == webRequest_.getHttpMethod() || HttpMethod.PATCH == webRequest_.getHttpMethod()) { if (content instanceof FormData) { ((FormData) content).fillRequest(webRequest_); } else { final String body = Context.toString(content); if (!body.isEmpty()) { if (LOG.isDebugEnabled()) { LOG.debug("Setting request body to: " + body); } webRequest_.setRequestBody(body); } } } } }
/** * Prepares the WebRequest that will be sent. * @param content the content to send */ private void prepareRequest(final Object content) { if (content != null && content != Undefined.instance) { if (!"".equals(content) && HttpMethod.GET == webRequest_.getHttpMethod()) { webRequest_.setHttpMethod(HttpMethod.POST); } if (HttpMethod.POST == webRequest_.getHttpMethod() || HttpMethod.PUT == webRequest_.getHttpMethod() || HttpMethod.PATCH == webRequest_.getHttpMethod()) { if (content instanceof FormData) { ((FormData) content).fillRequest(webRequest_); } else { final String body = Context.toString(content); if (!body.isEmpty()) { if (LOG.isDebugEnabled()) { LOG.debug("Setting request body to: " + body); } webRequest_.setRequestBody(body); } } } } }
WebClient client = new WebClient(BrowserVersion.FIREFOX_3_6); client.setTimeout(60000); client.setRedirectEnabled(true); client.setJavaScriptEnabled(true); client.setThrowExceptionOnFailingStatusCode(false); client.setThrowExceptionOnScriptError(false); client.setCssEnabled(false); client.setUseInsecureSSL(true); HtmlPage page = null; try { page = client.getPage("http://www.whatever.com"); } catch (Exception e) { // TODO Auto-generated catch block } if (page.getWebResponse().getStatusCode() == 404) { System.out.println("Page not found"); } // Post a request WebRequest request = new WebRequest(new URL("http://www.whatever.com/post_url")); request.setHttpMethod(HttpMethod.POST); List<NameValuePair> params = new ArrayList<NameValuePair>(); params.add(new NameValuePair("login", userLogin)); params.add(new NameValuePair("pass", userPassword)); request.setRequestParameters(params); page = client.getPage(request);
request.setHttpMethod(HttpMethod.valueOf(method.toUpperCase(Locale.ROOT)));
request.setAdditionalHeader(HttpHeader.REFERER, containingPage_.getUrl().toExternalForm()); request.setHttpMethod(HttpMethod.valueOf(method.toUpperCase(Locale.ROOT)));
/** * HtmlUnit을 이용한 HTML 코드 파싱. * * @param eachArchiveAddress 실제 만화가 담긴 아카이브 주소 * @return 성공 시 html 코드를 리턴 */ private String getHtmlPageHtmlUnit(String eachArchiveAddress) throws Exception { /* 필수! 로그 메세지 출력 안함 -> HtmlUnit 이용시 Verbose한 로그들이 너무 많아서 다 끔 */ java.util.logging.Logger.getLogger("com.gargoylesoftware").setLevel(Level.OFF); System.setProperty("org.apache.commons.logging.Log", "org.apache.commons.logging.impl.NoOpLog"); print.info("일반 연결 시도중...\n"); WebClient webClient = new WebClient(); webClient.getOptions().setRedirectEnabled(true); WebRequest req = new WebRequest(new URL(eachArchiveAddress)); req.setHttpMethod(HttpMethod.POST); req.setAdditionalHeader("User-Agent", UserAgent.getUserAgent()); req.setAdditionalHeader("Accept-Encoding", "gzip"); //20171126 gzip 추가 req.getRequestParameters().add(new NameValuePair("pass", PASSWORD)); //비밀번호 post 방식 전송 HtmlPage page = webClient.getPage(req); //Html코드를 포함한 페이지 소스코드가 담길 스트링 String pageSource = page.asXml(); /** 여기도 페이지 파싱 실패 시 검증하는 코드 들어가야 됨 **/ webClient.close(); print.info("일반 연결 성공\n"); return pageSource; }
request.setHttpMethod(HttpMethod.valueOf(method.toUpperCase(Locale.ROOT)));
request.setAdditionalHeader(HttpHeader.REFERER, containingPage_.getUrl().toExternalForm()); request.setHttpMethod(HttpMethod.valueOf(method.toUpperCase(Locale.ROOT)));
request.setHttpMethod(method); request.setRequestParameters(parameters); if (HttpMethod.POST == method) {
wrs.setHttpMethod(HttpMethod.HEAD);
webRequest.setHttpMethod(HttpMethod.POST); } else { webRequest.setHttpMethod(HttpMethod.GET);
request.setHttpMethod(HttpMethod.POST);