Java Code Examples for org.jsoup.Connection#Response

The following examples show how to use org.jsoup.Connection#Response . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
/**
 * 根据av号获取相关信息
 */
private void downloadByAv(String avNumber) throws Exception {
    if (avNumber.isEmpty()){
        sendToastMessage("请输入av号");
    }else {
        sendLogMessage("开始转换URL...\n");
        String url = "https://www.bilibili.com/video/av"+avNumber;
        Connection.Response response = Jsoup.connect(url).timeout(10000).execute();
        //普通视频不会重定向,番剧会进行重定向
        if (!response.url().toString().equals(url)){
            url = response.url().toString();
        }
        sendLogMessage("转换URL成功\n");
        downloadByUrl(url);
    }
}
 
Example 2
Source Project: G-Earth   File: HarbleAPIFetcher.java    License: MIT License 6 votes vote down vote up
public synchronized static void fetch(String hotelversion) {
    String cacheName = CACHE_PREFIX + hotelversion;

    if (Cacher.cacheFileExists(cacheName)) {
        HARBLEAPI = new HarbleAPI(hotelversion);
    }
    else {
        Connection connection = Jsoup.connect(HARBLE_API_URL.replace("$hotelversion$", hotelversion)).ignoreContentType(true);
        try {
            connection.timeout(3000);
            Connection.Response response = connection.execute();
            if (response.statusCode() == 200) {
                String messagesBodyJson = response.body();
                Cacher.updateCache(messagesBodyJson, cacheName);
                HARBLEAPI = new HarbleAPI(hotelversion);
            }
            else {
                HARBLEAPI = null;
            }
        } catch (IOException e) {
            HARBLEAPI = null;
        }

    }
}
 
Example 3
@Test
public void maxBodySize() throws IOException {
    String url = "http://direct.infohound.net/tools/large.html"; // 280 K

    Connection.Response defaultRes = Jsoup.connect(url).execute();
    Connection.Response smallRes = Jsoup.connect(url).maxBodySize(50 * 1024).execute(); // crops
    Connection.Response mediumRes = Jsoup.connect(url).maxBodySize(200 * 1024).execute(); // crops
    Connection.Response largeRes = Jsoup.connect(url).maxBodySize(300 * 1024).execute(); // does not crop
    Connection.Response unlimitedRes = Jsoup.connect(url).maxBodySize(0).execute();

    int actualDocText = 269541;
    assertEquals(actualDocText, defaultRes.parse().text().length());
    assertEquals(47200, smallRes.parse().text().length());
    assertEquals(196577, mediumRes.parse().text().length());
    assertEquals(actualDocText, largeRes.parse().text().length());
    assertEquals(actualDocText, unlimitedRes.parse().text().length());
}
 
Example 4
/**
 * Method which retrieve the cookie saying that we are logged in
 */
@Override
public boolean login() throws IOException {
    try {
        Connection.Response loginPage = this.newConnection(this.mShaarliUrl, Connection.Method.POST)
                .data("login", this.mUsername)
                .data("password", this.mPassword)
                .data("token", this.mToken)
                .data("returnurl", this.mShaarliUrl)
                .execute();

        this.mCookies = loginPage.cookies();
        loginPage.parse().body().select("a[href=?do=logout]").first()
                .attr("href"); // If this fails, you're not connected

    } catch (NullPointerException e) {
        return false;
    }
    return true;
}
 
Example 5
@Test
public void ignores500tExceptionIfSoConfigured() throws IOException {
    Connection con = Jsoup.connect("http://direct.infohound.net/tools/500.pl").ignoreHttpErrors(true);
    Connection.Response res = con.execute();
    Document doc = res.parse();
    assertEquals(500, res.statusCode());
    assertEquals("Application Error", res.statusMessage());
    assertEquals("Woops", doc.select("h1").first().text());
}
 
Example 6
@Test public void multipleParsesOkAfterBufferUp() throws IOException {
    Connection.Response res = Jsoup.connect(echoURL).execute().bufferUp();

    Document doc = res.parse();
    assertTrue(doc.title().contains("Environment"));

    Document doc2 = res.parse();
    assertTrue(doc2.title().contains("Environment"));
}
 
Example 7
@Test
public void redirectsResponseCookieToNextResponse() throws IOException {
    Connection con = Jsoup.connect("http://direct.infohound.net/tools/302-cookie.pl");
    Connection.Response res = con.execute();
    assertEquals("asdfg123", res.cookie("token")); // confirms that cookies set on 1st hit are presented in final result
    Document doc = res.parse();
    assertEquals("token=asdfg123; uid=jhy", ihVal("HTTP_COOKIE", doc)); // confirms that redirected hit saw cookie
}
 
Example 8
@Test
public void handlesDodgyCharset() throws IOException {
    // tests that when we get back "UFT8", that it is recognised as unsupported, and falls back to default instead
    String url = "http://direct.infohound.net/tools/bad-charset.pl";
    Connection.Response res = Jsoup.connect(url).execute();
    assertEquals("text/html; charset=UFT8", res.header("Content-Type")); // from the header
    assertEquals(null, res.charset()); // tried to get from header, not supported, so returns null
    Document doc = res.parse(); // would throw an error if charset unsupported
    assertTrue(doc.text().contains("Hello!"));
    assertEquals("UTF-8", res.charset()); // set from default on parse
}
 
Example 9
static Map<String, String> connect() throws IOException {

        // Connection.Response loginForm =
        // Jsoup.connect("https://passport.weibo.cn/signin/login")
        // .method(Connection.Method.GET)
        // .execute();
        //
        // Connection.Response res =
        // Jsoup.connect("https://passport.weibo.cn/signin/login")
        // .data("username", "18241141433", "password", "152300")
        // .data("ec", "0", "entry", "mweibo")
        // .data("mainpageflag", "1", "savestate", "1")
        // .timeout(30 * 1000)
        // .userAgent("Mozilla/5.0")
        // .cookies(loginForm.cookies())
        // .method(Method.POST)
        // .execute();
        // Document doc = res.parse();
        // System.out.println(doc);

        Connection.Response loginForm = Jsoup.connect("https://www.oschina.net/home/login")
                .method(Connection.Method.GET).execute();

        Connection.Response res = Jsoup.connect("https://www.oschina.net/home/login").header("Host", "www.oschina.net")
                .userAgent("Mozilla/5.0 (Windows NT 6.1; WOW64; rv:52.0) Gecko/20100101 Firefox/52.0")
                .referrer("https://www.oschina.net/home/login")
                .data("username", "[email protected]", "password", "lvmeng152300").data("save_login", "1")
                .timeout(30 * 1000).cookies(loginForm.cookies()).method(Method.POST).execute();
        Document doc = res.parse();
        System.out.println(doc);

        Map<String, String> loginCookies = res.cookies();
        String sessionId = res.cookie("SESSIONID");

        return loginCookies;
    }
 
Example 10
@Test
public void multiCookieSet() throws IOException {
    Connection con = Jsoup.connect("http://direct.infohound.net/tools/302-cookie.pl");
    Connection.Response res = con.execute();

    // test cookies set by redirect:
    Map<String, String> cookies = res.cookies();
    assertEquals("asdfg123", cookies.get("token"));
    assertEquals("jhy", cookies.get("uid"));

    // send those cookies into the echo URL by map:
    Document doc = Jsoup.connect(echoURL).cookies(cookies).get();
    assertEquals("token=asdfg123; uid=jhy", ihVal("HTTP_COOKIE", doc));
}
 
Example 11
@Test
public void ignores500NoWithContentExceptionIfSoConfigured() throws IOException {
    Connection con = Jsoup.connect("http://direct.infohound.net/tools/500-no-content.pl").ignoreHttpErrors(true);
    Connection.Response res = con.execute();
    Document doc = res.parse();
    assertEquals(500, res.statusCode());
    assertEquals("Application Error", res.statusMessage());
}
 
Example 12
@Test
public void inWildUtfRedirect() throws IOException {
    Connection.Response res = Jsoup.connect("http://brabantn.ws/Q4F").execute();
    Document doc = res.parse();
    assertEquals(
        "http://www.omroepbrabant.nl/?news/2474781303/Gestrande+ree+in+Oss+niet+verdoofd,+maar+doodgeschoten+%E2%80%98Dit+kan+gewoon+niet,+bizar%E2%80%99+[VIDEO].aspx",
        doc.location()
        );
}
 
Example 13
Source Project: superword   File: DynamicIp.java    License: Apache License 2.0 5 votes vote down vote up
public static boolean execute(Map<String, String> cookies, String action){
    String url = "http://192.168.0.1/goform/SysStatusHandle";
    Map<String, String> map = new HashMap<>();
    map.put("action", action);
    map.put("CMD", "WAN_CON");
    map.put("GO", "system_status.asp");
    Connection conn = Jsoup.connect(url)
            .header("Accept", ACCEPT)
            .header("Accept-Encoding", ENCODING)
            .header("Accept-Language", LANGUAGE)
            .header("Connection", CONNECTION)
            .header("Host", HOST)
            .header("Referer", REFERER)
            .header("User-Agent", USER_AGENT)
            .ignoreContentType(true)
            .timeout(30000);
    for(String cookie : cookies.keySet()){
        conn.cookie(cookie, cookies.get(cookie));
    }

    String title = null;
    try {
        Connection.Response response = conn.method(Connection.Method.POST).data(map).execute();
        String html = response.body();
        Document doc = Jsoup.parse(html);
        title = doc.title();
        LOGGER.info("操作连接页面标题:"+title);
        Thread.sleep(10000);
    }catch (Exception e){
        LOGGER.error(e.getMessage());
    }
    if("LAN | LAN Settings".equals(title)){
        if(("3".equals(action) && isConnected())
                || ("4".equals(action) && !isConnected())){
            return true;
        }
    }
    return false;
}
 
Example 14
Source Project: SteamGifts   File: PostCommentTask.java    License: MIT License 5 votes vote down vote up
@Override
protected void onPostExecute(Connection.Response response) {
    if (response != null && response.statusCode() == 301) {
        onSuccess();
    } else
        onFail();
}
 
Example 15
@Test
public void doesPut() throws IOException {
    Connection.Response res = Jsoup.connect(echoURL)
            .data("uname", "Jsoup", "uname", "Jonathan", "百", "度一下")
            .cookie("auth", "token")
            .method(Connection.Method.PUT)
            .execute();

    Document doc = res.parse();
    assertEquals("PUT", ihVal("REQUEST_METHOD", doc));
    //assertEquals("gzip", ihVal("HTTP_ACCEPT_ENCODING", doc)); // current proxy removes gzip on post
    assertEquals("auth=token", ihVal("HTTP_COOKIE", doc));
}
 
Example 16
Source Project: J-Kinopoisk2IMDB   File: ResponseMock.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public Connection.Response removeCookie(String name) {
    return null;
}
 
Example 17
@Test
public void shouldParseBrokenHtml5MetaCharsetTagCorrectly() throws IOException {
    Connection.Response res = Jsoup.connect("http://9kuhkep.net").execute();
    res.parse(); // would throw an error if charset unsupported
    assertEquals("UTF-8", res.charset());
}
 
Example 18
@Test
public void shouldEmptyMetaCharsetCorrectly() throws IOException {
    Connection.Response res = Jsoup.connect("http://aastmultimedia.com").execute();
    res.parse(); // would throw an error if charset unsupported
    assertEquals("UTF-8", res.charset());
}
 
Example 19
Source Project: J-Kinopoisk2IMDB   File: ResponseMock.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public Connection.Response removeHeader(String name) {
    return null;
}
 
Example 20
/**
 * Parses response, implementation specific to each Format
 *
 * @param response Response
 * @return List of movies if data not an empty string, or {@link Collections#emptyList()} otherwise.
 */
List<Movie> parseResponse(@NonNull final Connection.Response response);