Java Code Examples for org.jsoup.Connection#Response

The following examples show how to use org.jsoup.Connection#Response . 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
Source File: BiliBiliDownloadDialog.java    From DanDanPlayForAndroid with MIT License 6 votes vote down vote up
/**
 * 根据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 File: HarbleAPIFetcher.java    From G-Earth with 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
Source File: UrlConnectTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@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
Source File: PasswordNetworkManager.java    From Shaarlier with GNU General Public License v3.0 6 votes vote down vote up
/**
 * 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
Source File: UrlConnectTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@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 6
Source File: UrlConnectTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@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
Source File: PostCommentTask.java    From SteamGifts with 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 8
Source File: DynamicIp.java    From superword with 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 9
Source File: UrlConnectTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@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 10
Source File: UrlConnectTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@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 11
Source File: UrlConnectTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@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 12
Source File: WeiboCNLoginApater.java    From crawler-jsoup-maven with Apache License 2.0 5 votes vote down vote up
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 13
Source File: UrlConnectTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@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 14
Source File: UrlConnectTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@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 15
Source File: UrlConnectTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@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 16
Source File: UrlConnectTest.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
@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 17
Source File: ResponseMock.java    From J-Kinopoisk2IMDB with Apache License 2.0 4 votes vote down vote up
@Override
public Connection.Response removeHeader(String name) {
    return null;
}
 
Example 18
Source File: UrlConnectTest.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
@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 19
Source File: ResponseMock.java    From J-Kinopoisk2IMDB with Apache License 2.0 4 votes vote down vote up
@Override
public Connection.Response removeCookie(String name) {
    return null;
}
 
Example 20
Source File: ExchangeStrategy.java    From J-Kinopoisk2IMDB with Apache License 2.0 2 votes vote down vote up
/**
 * 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);