当前位置: 首页>>代码示例>>Java>>正文


Java WebConnectionWrapper类代码示例

本文整理汇总了Java中com.gargoylesoftware.htmlunit.util.WebConnectionWrapper的典型用法代码示例。如果您正苦于以下问题:Java WebConnectionWrapper类的具体用法?Java WebConnectionWrapper怎么用?Java WebConnectionWrapper使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


WebConnectionWrapper类属于com.gargoylesoftware.htmlunit.util包,在下文中一共展示了WebConnectionWrapper类的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: createWebClient

import com.gargoylesoftware.htmlunit.util.WebConnectionWrapper; //导入依赖的package包/类
private static WebClient createWebClient(final String url) {
    WebClient webClient = new WebClient(BrowserVersion.CHROME);

    // http://stackoverflow.com/a/23482615/2246865 by Neil McGuigan
    java.util.logging.Logger.getLogger("com.gargoylesoftware.htmlunit").setLevel(Level.OFF);

    webClient.setCssErrorHandler(new SilentCssErrorHandler());

    webClient.setIncorrectnessListener(new IncorrectnessListener() {
        @Override
        public void notify(String arg0, Object arg1) {
        }
    });

    webClient.setCookieManager(cookieManager);
    webClient.getOptions().setCssEnabled(false);
    webClient.getOptions().setThrowExceptionOnFailingStatusCode(false);
    webClient.getOptions().setThrowExceptionOnScriptError(false);
    webClient.getOptions().setPrintContentOnFailingStatusCode(false);
    webClient.getOptions().setUseInsecureSSL(true);

    // http://stackoverflow.com/a/14227559/2246865 by Lee
    webClient.setWebConnection(new WebConnectionWrapper(webClient) {
        @Override
        public WebResponse getResponse(final WebRequest request) throws IOException {
            if (request.getUrl().toString().contains(getDomainName(url))) {
                return super.getResponse(request);
            } else {
                return new StringWebResponse("", request.getUrl());
            }
        }
    });

    return webClient;
}
 
开发者ID:hurik,项目名称:MangaManagerAndDownloader,代码行数:36,代码来源:HTMLUnitHelper.java

示例2: StackExchangeChat

import com.gargoylesoftware.htmlunit.util.WebConnectionWrapper; //导入依赖的package包/类
public StackExchangeChat() {
    webClient = new WebClient(BrowserVersion.CHROME);
    webClient.getCookieManager().setCookiesEnabled(true);
    webClient.getOptions().setRedirectEnabled(true);
    webClient.getOptions().setJavaScriptEnabled(true);
    webClient.getOptions().setThrowExceptionOnScriptError(false);
    webClient.setWebConnection(new WebConnectionWrapper(webClient));

    rnd = new Random(System.nanoTime());
}
 
开发者ID:Unihedro,项目名称:JavaBot,代码行数:11,代码来源:StackExchangeChat.java

示例3: LinkedInOAuthRequestFilter

import com.gargoylesoftware.htmlunit.util.WebConnectionWrapper; //导入依赖的package包/类
@SuppressWarnings("deprecation")
public LinkedInOAuthRequestFilter(OAuthParams oAuthParams, Map<String, Object> httpParams,
                                  boolean lazyAuth, String[] enabledProtocols) {

    this.oAuthParams = oAuthParams;
    this.oAuthToken = null;

    // create HtmlUnit client
    webClient = new WebClient(BrowserVersion.FIREFOX_38);
    final WebClientOptions options = webClient.getOptions();
    options.setRedirectEnabled(true);
    options.setJavaScriptEnabled(false);
    options.setThrowExceptionOnFailingStatusCode(true);
    options.setThrowExceptionOnScriptError(true);
    options.setPrintContentOnFailingStatusCode(LOG.isDebugEnabled());
    options.setSSLClientProtocols(enabledProtocols);

    // add HTTP proxy if set
    if (httpParams != null && httpParams.get(ConnRoutePNames.DEFAULT_PROXY) != null) {
        final HttpHost proxyHost = (HttpHost) httpParams.get(ConnRoutePNames.DEFAULT_PROXY);
        final Boolean socksProxy = (Boolean) httpParams.get("http.route.socks-proxy");
        final ProxyConfig proxyConfig = new ProxyConfig(proxyHost.getHostName(), proxyHost.getPort(),
            socksProxy != null ? socksProxy : false);
        options.setProxyConfig(proxyConfig);
    }

    // disable default gzip compression, as error pages are sent with no compression and htmlunit doesn't negotiate
    new WebConnectionWrapper(webClient) {
        @Override
        public WebResponse getResponse(WebRequest request) throws IOException {
            request.setAdditionalHeader(HttpHeaders.ACCEPT_ENCODING, "identity");
            return super.getResponse(request);
        }
    };

    if (!lazyAuth) {
        try {
            updateOAuthToken();
        } catch (IOException e) {
            throw new IllegalArgumentException(
                String.format("Error authorizing user %s: %s", oAuthParams.getUserName(), e.getMessage()), e);
        }
    }
}
 
开发者ID:HydAu,项目名称:Camel,代码行数:45,代码来源:LinkedInOAuthRequestFilter.java

示例4: retrieveAccessToken

import com.gargoylesoftware.htmlunit.util.WebConnectionWrapper; //导入依赖的package包/类
/**
 * Initiates OAuth authorization using a mock chrome browser and retrieves and access token
 * @throws Exception
 */
private void retrieveAccessToken() throws Exception {
    final WebClient webClient = new WebClient(BrowserVersion.CHROME);
    webClient.setWebConnection(new WebConnectionWrapper(webClient) {
        @Override
        public WebResponse getResponse(final WebRequest request) throws IOException {

            WebResponse response = super.getResponse(request);
            String location = response.getResponseHeaderValue("location");
            if (response.getStatusCode() == 302 && location != null && location.startsWith(client.getClientRedirectUri())) {

                location = location.replace(client.getClientRedirectUri() + "?", "");
                String[] nameValues = location.split("&");
                for (String nameValue : nameValues) {
                    int idx = nameValue.indexOf("=");
                    if ("code".equals(URLDecoder.decode(nameValue.substring(0, idx), "UTF-8"))) {
                        String authorizationCode = URLDecoder.decode(nameValue.substring(idx + 1), "UTF-8");
                        AccessToken accessToken = client.getAuthService().requestAccessToken(clientId, clientSecret, redirectUrl, UberAuthService.GRANT_TYPE_AUTHORIZATION_CODE, authorizationCode, null);
                        client.setAccessToken(accessToken.getAccessToken());
                        client.setRefreshToken(accessToken.getRefreshToken());
                        break;
                    }
                }

                //return empty response
                request.setUrl(new URL("http://redirect"));
                WebResponseData data = new WebResponseData("".getBytes(), 200, "OK", Collections.EMPTY_LIST);
                return new WebResponse(data, request, 10);

            }
            return response;
        }
    });
    webClient.setAjaxController(new NicelyResynchronizingAjaxController());
    webClient.getOptions().setRedirectEnabled(true);
    webClient.getOptions().setThrowExceptionOnScriptError(false);
    webClient.getOptions().setThrowExceptionOnFailingStatusCode(false);
    webClient.getOptions().setCssEnabled(true);
    webClient.getOptions().setJavaScriptEnabled(false);
    webClient.getOptions().setUseInsecureSSL(true);
    webClient.getCookieManager().setCookiesEnabled(true);

    String authUrl = Utils.generateAuthorizeUrl(client.getOAuthUri(), clientId, new String[]{UberAuthService.SCOPE_PROFILE, UberAuthService.SCOPE_HISTORY_LITE, UberAuthService.SCOPE_REQUEST});
    final HtmlPage loginPage = webClient.getPage(authUrl);
    final HtmlForm form = loginPage.getForms().get(0);
    final HtmlTextInput emailInputText = form.getInputByName("email");
    final HtmlPasswordInput passwordInputText = form.getInputByName("password");
    final HtmlButton submitButton = (HtmlButton) form.getByXPath("//button[@type='submit']").get(0);
    emailInputText.setText(username);
    passwordInputText.setText(password);
    final Page scopePage = submitButton.click();
    /**
     * If the user already accepted scope permissions, the accept screen is skipped and the user will be forwarded to the authorization code redirect
     */
    //check for the Accept html page
    if (scopePage instanceof HtmlPage) {
        //click accept button
        if (((HtmlPage)scopePage).getForms().size() > 0) {
            final HtmlForm form2 = ((HtmlPage) scopePage).getForms().get(0);
            final HtmlButton allowButton = (HtmlButton) form2.getByXPath("//button[@value='yes']").get(0);
            allowButton.click();
        }
    }
}
 
开发者ID:vsima,项目名称:uber-java-client,代码行数:68,代码来源:SandboxServerTest.java


注:本文中的com.gargoylesoftware.htmlunit.util.WebConnectionWrapper类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。