Search in sources :

Example 41 with Connection

use of org.jsoup.Connection in project jsoup by jhy.

the class HttpConnectionTest method referrer.

@Test
public void referrer() {
    Connection con = HttpConnection.connect("http://example.com/");
    con.referrer("http://foo.com");
    assertEquals("http://foo.com", con.request().header("Referer"));
}
Also used : Connection(org.jsoup.Connection) Test(org.junit.Test) ParseTest(org.jsoup.integration.ParseTest)

Example 42 with Connection

use of org.jsoup.Connection in project jsoup by jhy.

the class HttpConnectionTest method throwsExceptionOnBodyWithoutExecute.

@Test(expected = IllegalArgumentException.class)
public void throwsExceptionOnBodyWithoutExecute() throws IOException {
    Connection con = HttpConnection.connect("http://example.com");
    con.response().body();
}
Also used : Connection(org.jsoup.Connection) Test(org.junit.Test) ParseTest(org.jsoup.integration.ParseTest)

Example 43 with Connection

use of org.jsoup.Connection in project jsoup by jhy.

the class HttpConnectionTest method data.

@Test
public void data() {
    Connection con = HttpConnection.connect("http://example.com/");
    con.data("Name", "Val", "Foo", "bar");
    Collection<Connection.KeyVal> values = con.request().data();
    Object[] data = values.toArray();
    Connection.KeyVal one = (Connection.KeyVal) data[0];
    Connection.KeyVal two = (Connection.KeyVal) data[1];
    assertEquals("Name", one.key());
    assertEquals("Val", one.value());
    assertEquals("Foo", two.key());
    assertEquals("bar", two.value());
}
Also used : Connection(org.jsoup.Connection) Test(org.junit.Test) ParseTest(org.jsoup.integration.ParseTest)

Example 44 with Connection

use of org.jsoup.Connection in project AisenWeiBo by wangdan.

the class SDK method doLike.

/**
     * 点赞
     *
     * @param statusId
     * @param like
     * @param cookie
     * @return
     * @throws TaskException
     */
public LikeResultBean doLike(String statusId, boolean like, String cookie) throws TaskException {
    try {
        String url = like ? "http://m.weibo.cn/attitudesDeal/add" : "http://m.weibo.cn/attitudesDeal/delete";
        Map<String, String> cookieMap = new HashMap<String, String>();
        String[] cookieValues = cookie.split(";");
        for (String cookieValue : cookieValues) {
            String key = cookieValue.split("=")[0];
            String value = cookieValue.split("=")[1];
            cookieMap.put(key, value);
        }
        //            Logger.d(WeiboClientActivity.TAG, cookieMap);
        Connection connection = Jsoup.connect(url);
        connection.userAgent("Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:33.0) Gecko/20100101 Firefox/33.0").referrer("http://m.weibo.cn/").cookies(cookieMap).data("id", statusId).method(Connection.Method.POST);
        if (like)
            connection.data("attitude", "heart");
        String body = connection.execute().body();
        if (!TextUtils.isEmpty(body)) {
            Logger.d(TAG, body);
            if (body.indexOf("http://passport.weibo.cn/sso/crossdomain") != -1)
                throw new TaskException("-100", "未登录");
            else if (body.indexOf("<html") != -1)
                throw new TaskException("-100", "未登录");
            LikeResultBean likeBean = JSON.parseObject(body, LikeResultBean.class);
            if (likeBean.getOk() == 1) {
                return likeBean;
            } else if (likeBean.getOk() == -100) {
                throw new TaskException("-100", "未登录");
            } else {
                throw new TaskException("", likeBean.getMsg());
            }
        }
    } catch (Exception e) {
        if (e instanceof TaskException)
            throw (TaskException) e;
        e.printStackTrace();
    }
    throw new TaskException(TaskException.TaskError.timeout.toString());
}
Also used : LikeResultBean(org.aisen.weibo.sina.support.bean.LikeResultBean) TaskException(org.aisen.android.network.task.TaskException) HashMap(java.util.HashMap) Connection(org.jsoup.Connection) TaskException(org.aisen.android.network.task.TaskException)

Example 45 with Connection

use of org.jsoup.Connection in project jabref by JabRef.

the class DoiResolution method findFullText.

@Override
public Optional<URL> findFullText(BibEntry entry) throws IOException {
    Objects.requireNonNull(entry);
    Optional<URL> pdfLink = Optional.empty();
    Optional<DOI> doi = entry.getField(FieldName.DOI).flatMap(DOI::parse);
    if (doi.isPresent()) {
        String sciLink = doi.get().getURIAsASCIIString();
        // follow all redirects and scan for a single pdf link
        if (!sciLink.isEmpty()) {
            try {
                Connection connection = Jsoup.connect(sciLink);
                // pretend to be a browser (agent & referrer)
                connection.userAgent(URLDownload.USER_AGENT);
                connection.referrer("http://www.google.com");
                connection.followRedirects(true);
                connection.ignoreHttpErrors(true);
                // some publishers are quite slow (default is 3s)
                connection.timeout(5000);
                Document html = connection.get();
                // scan for PDF
                Elements elements = html.body().select("a[href]");
                List<Optional<URL>> links = new ArrayList<>();
                for (Element element : elements) {
                    String href = element.attr("abs:href").toLowerCase(Locale.ENGLISH);
                    String hrefText = element.text().toLowerCase(Locale.ENGLISH);
                    // See https://github.com/lehner/LocalCopy for more scrape ideas
                    if ((href.contains("pdf") || hrefText.contains("pdf")) && new URLDownload(href).isPdf()) {
                        links.add(Optional.of(new URL(href)));
                    }
                }
                // return if only one link was found (high accuracy)
                if (links.size() == 1) {
                    LOGGER.info("Fulltext PDF found @ " + sciLink);
                    pdfLink = links.get(0);
                }
            } catch (IOException e) {
                LOGGER.warn("DoiResolution fetcher failed: ", e);
            }
        }
    }
    return pdfLink;
}
Also used : Optional(java.util.Optional) Element(org.jsoup.nodes.Element) Connection(org.jsoup.Connection) ArrayList(java.util.ArrayList) IOException(java.io.IOException) Document(org.jsoup.nodes.Document) Elements(org.jsoup.select.Elements) URLDownload(org.jabref.logic.net.URLDownload) URL(java.net.URL) DOI(org.jabref.model.entry.identifier.DOI)

Aggregations

Connection (org.jsoup.Connection)47 Test (org.junit.Test)42 Document (org.jsoup.nodes.Document)26 ParseTest (org.jsoup.integration.ParseTest)13 IOException (java.io.IOException)3 URL (java.net.URL)3 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 Element (org.jsoup.nodes.Element)2 Elements (org.jsoup.select.Elements)2 JsonArray (com.google.gson.JsonArray)1 JsonObject (com.google.gson.JsonObject)1 JsonParser (com.google.gson.JsonParser)1 JsonSyntaxException (com.google.gson.JsonSyntaxException)1 News (com.itculturalfestival.smartcampus.entity.News)1 DisposableHolder (com.vegen.smartcampus.baseframework.network.DisposableHolder)1 ObservableOnSubscribe (io.reactivex.ObservableOnSubscribe)1 Disposable (io.reactivex.disposables.Disposable)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1