Java Code Examples for org.jsoup.Connection#cookie()

The following examples show how to use org.jsoup.Connection#cookie() . 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: LoadGiveawayWinnersTask.java    From SteamGifts with MIT License 6 votes vote down vote up
@Override
protected List<Winner> doInBackground(Void... params) {
    Log.d(TAG, "Fetching giveaways for page " + page);

    try {
        // Fetch the Giveaway page

        Connection jsoup = Jsoup.connect("https://www.steamgifts.com/giveaway/" + path + "/winners/search")
                .userAgent(Constants.JSOUP_USER_AGENT)
                .timeout(Constants.JSOUP_TIMEOUT);
        jsoup.data("page", Integer.toString(page));

        if (SteamGiftsUserData.getCurrent(fragment.getContext()).isLoggedIn())
            jsoup.cookie("PHPSESSID", SteamGiftsUserData.getCurrent(fragment.getContext()).getSessionId());
        Document document = jsoup.get();

        SteamGiftsUserData.extract(fragment.getContext(), document);

        return loadAll(document);
    } catch (IOException e) {
        Log.e(TAG, "Error fetching URL", e);
        return null;
    }
}
 
Example 2
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 3
Source File: LoadGameListTask.java    From SteamGifts with MIT License 5 votes vote down vote up
@Override
protected List<IEndlessAdaptable> doInBackground(Void... params) {
    try {
        // Fetch the Giveaway page

        Connection jsoup = Jsoup.connect("https://www.steamgifts.com/" + pathSegment + "/search")
                .userAgent(Constants.JSOUP_USER_AGENT)
                .timeout(Constants.JSOUP_TIMEOUT);
        jsoup.data("page", Integer.toString(page));

        if (searchQuery != null)
            jsoup.data("q", searchQuery);

        jsoup.cookie("PHPSESSID", SteamGiftsUserData.getCurrent(context).getSessionId());

        Document document = jsoup.get();

        SteamGiftsUserData.extract(context, document);

        // Fetch the xsrf token
        Element xsrfToken = document.select("input[name=xsrf_token]").first();
        if (xsrfToken != null)
            foundXsrfToken = xsrfToken.attr("value");

        // Do away with pinned giveaways.
        document.select(".pinned-giveaways__outer-wrap").html("");

        // Parse all rows of giveaways
        return loadAll(document);
    } catch (Exception e) {
        Log.e(TAG, "Error fetching URL", e);
        return null;
    }
}
 
Example 4
Source File: LoadUserDetailsTask.java    From SteamGifts with MIT License 5 votes vote down vote up
@Override
protected List<Giveaway> doInBackground(Void... params) {
    Log.d(TAG, "Fetching giveaways for user " + path + " on page " + page);

    try {
        // Fetch the Giveaway page
        Connection connection = Jsoup.connect("https://www.steamgifts.com/user/" + path + "/search")
                .userAgent(Constants.JSOUP_USER_AGENT)
                .timeout(Constants.JSOUP_TIMEOUT);
        connection.data("page", Integer.toString(page));
        if (SteamGiftsUserData.getCurrent(fragment.getContext()).isLoggedIn()) {
            connection.cookie("PHPSESSID", SteamGiftsUserData.getCurrent(fragment.getContext()).getSessionId());
            connection.followRedirects(false);
        }

        Connection.Response response = connection.execute();
        Document document = response.parse();

        if (response.statusCode() == 200) {

            SteamGiftsUserData.extract(fragment.getContext(), document);

            if (!user.isLoaded())
                foundXsrfToken = Utils.loadUserProfile(user, document);

            // Parse all rows of giveaways
            return Utils.loadGiveawaysFromList(document);
        } else {
            Log.w(TAG, "Got status code " + response.statusCode());
            return null;
        }
    } catch (Exception e) {
        Log.e(TAG, "Error fetching URL", e);
        return null;
    }
}
 
Example 5
Source File: LoadMessagesTask.java    From SteamGifts with MIT License 5 votes vote down vote up
@Override
protected List<IEndlessAdaptable> doInBackground(Void... params) {
    try {
        // Fetch the messages page

        Connection jsoup = Jsoup.connect("https://www.steamgifts.com/messages/search")
                .userAgent(Constants.JSOUP_USER_AGENT)
                .timeout(Constants.JSOUP_TIMEOUT);
        jsoup.data("page", Integer.toString(page));
        jsoup.cookie("PHPSESSID", SteamGiftsUserData.getCurrent(context).getSessionId());

        Document document = jsoup.get();

        SteamGiftsUserData.extract(context, document);

        // Fetch the xsrf token
        Element xsrfToken = document.select("input[name=xsrf_token]").first();
        if (xsrfToken != null)
            foundXsrfToken = xsrfToken.attr("value");

        // Parse all rows of giveaways
        return loadMessages(document);
    } catch (Exception e) {
        Log.e(TAG, "Error fetching URL", e);
        return null;
    }
}
 
Example 6
Source File: LoadWhitelistBlacklistTask.java    From SteamGifts with MIT License 5 votes vote down vote up
@Override
protected List<BasicUser> doInBackground(Void... params) {
    try {
        // Fetch the Giveaway page
        String url = "https://www.steamgifts.com/account/manage/" + what.name().toLowerCase(Locale.ENGLISH) + "/search";
        Log.d(TAG, "Fetching URL " + url);

        Connection jsoup = Jsoup.connect(url)
                .userAgent(Constants.JSOUP_USER_AGENT)
                .timeout(Constants.JSOUP_TIMEOUT)
                .followRedirects(false);
        jsoup.data("page", Integer.toString(page));

        if (searchQuery != null)
            jsoup.data("q", searchQuery);

        jsoup.cookie("PHPSESSID", SteamGiftsUserData.getCurrent(fragment.getContext()).getSessionId());

        Document document = jsoup.get();

        SteamGiftsUserData.extract(fragment.getContext(), document);

        // Fetch the xsrf token
        Element xsrfToken = document.select("input[name=xsrf_token]").first();
        if (xsrfToken != null)
            foundXsrfToken = xsrfToken.attr("value");

        // Do away with pinned giveaways.
        document.select(".pinned-giveaways__outer-wrap").html("");

        // Parse all rows of giveaways
        return loadAll(document);
    } catch (Exception e) {
        Log.e(TAG, "Error fetching URL", e);
        return null;
    }
}
 
Example 7
Source File: LoadDiscussionDetailsTask.java    From SteamGifts with MIT License 5 votes vote down vote up
private Connection.Response connect() throws IOException {
    String url = "https://www.steamgifts.com/discussion/" + discussionId + "/search?page=" + page;
    Log.v(TAG, "Fetching discussion details for " + url);
    Connection connection = Jsoup.connect(url)
            .userAgent(Constants.JSOUP_USER_AGENT)
            .timeout(Constants.JSOUP_TIMEOUT)
            .followRedirects(true);

    if (SteamGiftsUserData.getCurrent(fragment.getContext()).isLoggedIn())
        connection.cookie("PHPSESSID", SteamGiftsUserData.getCurrent(fragment.getContext()).getSessionId());

    return connection.execute();
}
 
Example 8
Source File: DynamicIp.java    From rank 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: LoadGiveawayListTask.java    From SteamGifts with MIT License 4 votes vote down vote up
@Override
protected List<Giveaway> doInBackground(Void... params) {
    Log.d(TAG, "Fetching giveaways for page " + page);

    try {
        // Fetch the Giveaway page

        Connection jsoup = Jsoup.connect("https://www.steamgifts.com/giveaways/search")
                .userAgent(Constants.JSOUP_USER_AGENT)
                .timeout(Constants.JSOUP_TIMEOUT);
        jsoup.data("page", Integer.toString(page));

        if (searchQuery != null)
            jsoup.data("q", searchQuery);

        FilterData filterData = FilterData.getCurrent(fragment.getContext());
        if (!filterData.isEntriesPerCopy()) {
            addFilterParameter(jsoup, "entry_max", filterData.getMaxEntries());
            addFilterParameter(jsoup, "entry_min", filterData.getMinEntries());
        }
        if (!filterData.isRestrictLevelOnlyOnPublicGiveaways()) {
            addFilterParameter(jsoup, "level_min", filterData.getMinLevel());
            addFilterParameter(jsoup, "level_max", filterData.getMaxLevel());
        }
        addFilterParameter(jsoup, "region_restricted", filterData.isRegionRestrictedOnly());
        addFilterParameter(jsoup, "copy_min", filterData.getMinCopies());
        addFilterParameter(jsoup, "copy_max", filterData.getMaxCopies());
        addFilterParameter(jsoup, "point_min", filterData.getMinPoints());
        addFilterParameter(jsoup, "point_max", filterData.getMaxPoints());

        if (type != GiveawayListFragment.Type.ALL)
            jsoup.data("type", type.name().toLowerCase(Locale.ENGLISH));

        if (SteamGiftsUserData.getCurrent(fragment.getContext()).isLoggedIn())
            jsoup.cookie("PHPSESSID", SteamGiftsUserData.getCurrent(fragment.getContext()).getSessionId());
        Document document = jsoup.get();

        SteamGiftsUserData.extract(fragment.getContext(), document);

        // Fetch the xsrf token
        Element xsrfToken = document.select("input[name=xsrf_token]").first();
        if (xsrfToken != null)
            foundXsrfToken = xsrfToken.attr("value");

        // Do away with pinned giveaways.
        if (!showPinnedGiveaways)
            document.select(".pinned-giveaways__outer-wrap").html("");

        // Parse all rows of giveaways
        return Utils.loadGiveawaysFromList(document);
    } catch (Exception e) {
        Log.e(TAG, "Error fetching URL", e);
        return null;
    }
}
 
Example 10
Source File: LoadDiscussionListTask.java    From SteamGifts with MIT License 4 votes vote down vote up
@Override
protected List<Discussion> doInBackground(Void... params) {
    try {
        // Fetch the Giveaway page
        String segment = "";
        if (type != DiscussionListFragment.Type.ALL)
            segment = type.name().replace("_", "-").toLowerCase(Locale.ENGLISH) + "/";
        String url = "https://www.steamgifts.com/discussions/" + segment + "search";

        Log.d(TAG, "Fetching discussions for page " + page + " and URL " + url);

        Connection jsoup = Jsoup.connect(url)
                .userAgent(Constants.JSOUP_USER_AGENT)
                .timeout(Constants.JSOUP_TIMEOUT);
        jsoup.data("page", Integer.toString(page));

        if (searchQuery != null)
            jsoup.data("q", searchQuery);

        // We do not want to follow redirects here, because SteamGifts redirects to the main (giveaways) page if we're not logged in.
        // For all other pages however, if we're not logged in, we're redirected once as well?
        if (type == DiscussionListFragment.Type.CREATED)
            jsoup.followRedirects(false);

        if (SteamGiftsUserData.getCurrent(fragment.getContext()).isLoggedIn())
            jsoup.cookie("PHPSESSID", SteamGiftsUserData.getCurrent(fragment.getContext()).getSessionId());
        Document document = jsoup.get();

        SteamGiftsUserData.extract(fragment.getContext(), document);

        // Parse all rows of discussions
        Elements discussions = document.select(".table__row-inner-wrap");
        Log.d(TAG, "Found inner " + discussions.size() + " elements");

        List<Discussion> discussionList = new ArrayList<>();
        for (Element element : discussions) {
            Element link = element.select("h3 a").first();

            // Basic information
            Uri uri = Uri.parse(link.attr("href"));
            String discussionId = uri.getPathSegments().get(1);
            String discussionName = uri.getPathSegments().get(2);

            Discussion discussion = new Discussion(discussionId);
            discussion.setTitle(link.text());
            discussion.setName(discussionName);

            Element p = element.select(".table__column--width-fill p").first();
            discussion.setCreatedTime(Integer.valueOf(p.select("span").first().attr("data-timestamp")));
            discussion.setCreator(p.select("a").last().text());

            // The creator's avatar
            Element avatarNode = element.select(".table_image_avatar").first();
            if (avatarNode != null)
                discussion.setCreatorAvatar(Utils.extractAvatar(avatarNode.attr("style")));

            discussion.setLocked(element.hasClass("is-faded"));
            discussion.setPoll(!element.select("h3 i.fa-align-left").isEmpty());
            discussionList.add(discussion);
        }
        return discussionList;
    } catch (Exception e) {
        Log.e(TAG, "Error fetching URL", e);
        return null;
    }
}
 
Example 11
Source File: LoadGiveawayGroupsTask.java    From SteamGifts with MIT License 4 votes vote down vote up
@Override
protected List<GiveawayGroup> doInBackground(Void... params) {
    Log.d(TAG, "Fetching giveaways for page " + page);

    try {
        // Fetch the Giveaway page

        Connection jsoup = Jsoup.connect("https://www.steamgifts.com/giveaway/" + path + "/groups/search")
                .userAgent(Constants.JSOUP_USER_AGENT)
                .timeout(Constants.JSOUP_TIMEOUT);
        jsoup.data("page", Integer.toString(page));

        if (SteamGiftsUserData.getCurrent(fragment.getContext()).isLoggedIn())
            jsoup.cookie("PHPSESSID", SteamGiftsUserData.getCurrent(fragment.getContext()).getSessionId());
        Document document = jsoup.get();

        SteamGiftsUserData.extract(fragment.getContext(), document);

        // Parse all rows of groups
        Elements groups = document.select(".table__row-inner-wrap");
        Log.d(TAG, "Found inner " + groups.size() + " elements");

        List<GiveawayGroup> groupList = new ArrayList<>();
        for (Element element : groups) {
            Element link = element.select(".table__column__heading").first();

            // Basic information
            String title = link.text();
            String id = link.attr("href").substring(7, 12);

            String avatar = null;
            Element avatarNode = element.select(".global__image-inner-wrap").first();
            if (avatarNode != null)
                avatar = Utils.extractAvatar(avatarNode.attr("style"));

            GiveawayGroup group = new GiveawayGroup(id, title, avatar);
            groupList.add(group);
        }

        return groupList;
    } catch (IOException e) {
        Log.e(TAG, "Error fetching URL", e);
        return null;
    }
}
 
Example 12
Source File: LoadGiveawayTask.java    From SteamGifts with MIT License 4 votes vote down vote up
@Override
protected Giveaway doInBackground(Void... params) {
    String url = "http://www.sgtools.info/giveaways/" + uuid.toString().toLowerCase(Locale.ENGLISH);

    try {
        Log.v(TAG, "Connecting to " + url);
        Connection connection = Jsoup
                .connect(url)
                .userAgent(Constants.JSOUP_USER_AGENT)
                .timeout(Constants.JSOUP_TIMEOUT)
                .followRedirects(false);

        String sessionId = SGToolsUserData.getCurrent().getSessionId();
        // We'll be redirected if it is null anyway...
        if (sessionId != null) {
            connection.cookie("PHPSESSID", sessionId);
        }

        Connection.Response response = connection.method(Connection.Method.GET).execute();

        Log.v(TAG, url + " returned Status Code " + response.statusCode() + " (" + response.statusMessage() + ")");

        if (response.statusCode() == 200) {
            Document document = response.parse();

            Giveaway giveaway = new Giveaway();
            giveaway.setName(document.select(".featured__heading__medium").first().text());

            // TODO if sgtools.info ever allows you to create a giveaway for a game not on the store (tested HiB #3), check something more here.
            // Right now, it's only a 500 while creating such a giveaway, thus impossible.
            Uri steamUri = Uri.parse(document.select("a.global__image-outer-wrap--game-large").first().attr("href"));
            if (steamUri != null) {
                List<String> pathSegments = steamUri.getPathSegments();
                if (pathSegments.size() >= 2)
                    giveaway.setGameId(Integer.parseInt(pathSegments.get(1)));
                giveaway.setType("app".equals(pathSegments.get(0)) ? Game.Type.APP : Game.Type.SUB);
            }

            // add all rules
            for (Element rule : document.select(".rules ul li"))
                giveaway.addRule(rule.text());

            Element gaUrl = document.select(".gaurl").first();
            if (gaUrl != null)
                giveawayUrl = gaUrl.text();

            return giveaway;
        }

        needsLogin = true;
        return null;
    } catch (Exception e) {
        Log.e(TAG, "Error fetching URL", e);
        return null;
    }
}
 
Example 13
Source File: HttpConnectionTest.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
@Test public void cookie() {
    Connection con = HttpConnection.connect("http://example.com/");
    con.cookie("Name", "Val");
    assertEquals("Val", con.request().cookie("Name"));
}
 
Example 14
Source File: HttpConnectionTest.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
@Test public void cookie() {
    Connection con = HttpConnection.connect("http://example.com/");
    con.cookie("Name", "Val");
    assertEquals("Val", con.request().cookie("Name"));
}
 
Example 15
Source File: HttpConnectionTest.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
@Test public void cookie() {
    Connection con = HttpConnection.connect("http://example.com/");
    con.cookie("Name", "Val");
    assertEquals("Val", con.request().cookie("Name"));
}
 
Example 16
Source File: HttpConnectionTest.java    From jsoup-learning with MIT License 4 votes vote down vote up
@Test public void cookie() {
    Connection con = HttpConnection.connect("http://example.com/");
    con.cookie("Name", "Val");
    assertEquals("Val", con.request().cookie("Name"));
}