本文整理汇总了Java中org.jsoup.Connection.headers方法的典型用法代码示例。如果您正苦于以下问题:Java Connection.headers方法的具体用法?Java Connection.headers怎么用?Java Connection.headers使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.jsoup.Connection
的用法示例。
在下文中一共展示了Connection.headers方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: load
import org.jsoup.Connection; //导入方法依赖的package包/类
/**
* 加载页面
*
* @param pageLoadInfo
*
* @return
*/
public static Document load(PageLoadInfo pageLoadInfo) {
if (!UrlUtil.isUrl(pageLoadInfo.getUrl())) {
return null;
}
try {
// 请求设置
Connection conn = Jsoup.connect(pageLoadInfo.getUrl());
if (pageLoadInfo.getParamMap() != null && !pageLoadInfo.getParamMap().isEmpty()) {
conn.data(pageLoadInfo.getParamMap());
}
if (pageLoadInfo.getCookieMap() != null && !pageLoadInfo.getCookieMap().isEmpty()) {
conn.cookies(pageLoadInfo.getCookieMap());
}
if (pageLoadInfo.getHeaderMap()!=null && !pageLoadInfo.getHeaderMap().isEmpty()) {
conn.headers(pageLoadInfo.getHeaderMap());
}
if (pageLoadInfo.getUserAgent()!=null) {
conn.userAgent(pageLoadInfo.getUserAgent());
}
if (pageLoadInfo.getReferrer() != null) {
conn.referrer(pageLoadInfo.getReferrer());
}
conn.timeout(pageLoadInfo.getTimeoutMillis());
// 代理
if (pageLoadInfo.getProxy() != null) {
conn.proxy(pageLoadInfo.getProxy());
}
// 发出请求
Document html = null;
if (pageLoadInfo.getIfPost()) {
html = conn.post();
} else {
html = conn.get();
}
return html;
} catch (IOException e) {
logger.error(e.getMessage(), e);
return null;
}
}
示例2: fetch
import org.jsoup.Connection; //导入方法依赖的package包/类
public Connection.Response fetch(Parameters params) throws IOException {
setProperty("sun.net.http.allowRestrictedHeaders", "true");
setProperty("javax.net.ssl.trustStore", "/etc/ssl/certs/java/cacerts");
int retryCount = 0;
while(true) {
try {
final Connection connection = Jsoup
.connect(params.getUrlToFetch())
.method(params.getMethod())
.validateTLSCertificates(false)
.ignoreHttpErrors(true)
.followRedirects(params.isFollowRedirects())
.ignoreContentType(GLOBAL_CONFIG.isIgnoringContentType())
.userAgent(params.getUserAgent())
.referrer(params.getReferrer())
.proxy(createProxy(params.getProxy()))
.maxBodySize(0)
.timeout(params.getTimeout());
if (!params.getCookie().isEmpty()) {
connection.cookies(params.getCookie());
}
if (!params.getHeaders().isEmpty()) {
connection.headers(params.getCookie());
}
if (!params.getRequestBody().isEmpty()) {
connection.requestBody(params.getRequestBody());
}
System.out.println("\uD83D\uDD3D " + ansi().fg(CYAN).bold().a("fetched page : ").reset() + params.getUrlToFetch());
return connection.execute();
} catch(SocketTimeoutException ste) {
if(retryCount > params.getRetriesOnTimeout()) {
throw ste;
}
System.out.println("\uD83D\uDD50 " + ansi().fg(YELLOW).bold().a("fetch timeout: ").reset() + "SocketRead time out after " + retryCount++ + ". try");
}
}
}