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"));
}
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();
}
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());
}
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());
}
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;
}
Aggregations