本文整理了Java中org.jsoup.Connection.header()
方法的一些代码示例,展示了Connection.header()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Connection.header()
方法的具体详情如下:
包路径:org.jsoup.Connection
类名称:Connection
方法名:header
[英]Set a request header.
[中]设置请求头。
代码示例来源:origin: RipMeApp/ripme
public Http header(String name, String value) {
connection.header(name, value);
return this;
}
public Http cookies(Map<String,String> cookies) {
代码示例来源:origin: jphp-group/jphp
@Signature
public Connection headers(Map<String, String> data) {
for (Map.Entry<String, String> entry : data.entrySet()) {
getWrappedObject().header(entry.getKey(), entry.getValue());
}
return getWrappedObject();
}
}
代码示例来源:origin: ysc/QuestionAnsweringSystem
try {
Document document = Jsoup.connect(url)
.header("Accept", ACCEPT)
.header("Accept-Encoding", ENCODING)
.header("Accept-Language", LANGUAGE)
.header("Connection", CONNECTION)
.header("User-Agent", USER_AGENT)
.header("Host", HOST)
.header("Referer", referer)
.get();
String resultCssQuery = "html > body > div > div > div > div > div";
代码示例来源:origin: RipMeApp/ripme
Document doc = Jsoup
.connect("https://vk.com/al_photos.php")
.header("Referer", this.url.toExternalForm())
.ignoreContentType(true)
.userAgent(USER_AGENT)
代码示例来源:origin: ChinaSilence/any-video
public static Document getDocWithPhone(String url, String cookie) {
try {
return Jsoup.connect(url).userAgent(UA_PHONE).timeout(TIME_OUT).header("Cookie", cookie).ignoreContentType(true).get();
} catch (IOException e) {
log.error(ERROR_DESC + url);
throw new AnyException(ERROR_DESC + url);
}
}
代码示例来源:origin: ChinaSilence/any-video
/**
* 获取片段播放的 key
*/
private String videoKey(String vid, String filename, String format) {
try {
Document document = Jsoup.connect(KEY_API).header("Cookie", COOKIE)
.data("vid", vid).data("platform", PLATFORM)
.data("otype", "json")
.data("filename", filename).data("sdtfrom", SDTFROM)
.data("format", format).data("guid", GUID).ignoreContentType(true).get();
String result = document.text().replace("QZOutputJson=", "");
System.out.println(result);
result = result.substring(0, result.length() - 1);
return JSONObject.parseObject(result).getString("key");
} catch (IOException e) {
log.info("request tencent video part api error, vid : " + vid);
throw new AnyException("request tencent api error, vid : " + vid);
}
}
}
代码示例来源:origin: ChinaSilence/any-video
/**
* 调用腾讯接口,获取视频信息
*/
private String videoInfo(String vid) {
try {
Document document = Jsoup.connect(VIDEO_API).header("Cookie", COOKIE)
.data("vids", vid).data("platform", PLATFORM)
.data("sdtfrom", SDTFROM)
.data("format", "10209")
.data("otype", "json").data("defn", "fhd")
.data("defaultfmt", "fhd").data("guid", GUID).ignoreContentType(true).get();
String result = document.text().replace("QZOutputJson=", "");
return result.substring(0, result.length() - 1);
} catch (IOException e) {
log.info("request tencent api error, vid : " + vid);
throw new AnyException("request tencent api error, vid : " + vid);
}
}
代码示例来源:origin: JinBoy23520/CoderToDeveloperByTCLer
/**
* 模拟浏览器行为的请求头获取Document
* @param url
* @return
* @throws IOException
*/
public static Document getDoc(String url) throws IOException {
/**
* 在爬之前最好看一下浏览器访问目标网站的Request Header信息,然后进行模仿
*/
return Jsoup.connect(url)
// .header("accept", "application/json, text/plain,*/*")
// .header("Accept-Encoding", "gzip, deflate,br")
// .header("Accept-Language", "zh-CN,zh;q=0.8")//,en-US;q=0.5,en;q=0.3
// .header("Referer", "https://www.baidu.com/")
.header("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8")
.header("Accept-Encoding", "gzip, deflate")
.header("Accept-Language", "zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3")//,en-US;q=0.5,en;q=0.3
.header("Cache-Control","max-age=0")
.header("Connection","keep-alive")
.header("Host", "www.cnblogs.com")
.header("Referer","http://www.cnblogs.com/WangHaiMing/")
.header("Upgrade-Insecure-Requests","1")
.header("User-Agent","Mozilla/5.0 (Windows NT 10.0; WOW64; rv:55.0) Gecko/20100101 Firefox/55.0")// "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:48.0) Gecko/20100101 Firefox/48.0
.header("Cookie", "_ga=GA1.2.727269871.1498415016")
.timeout(5000)
.get();
}
}
代码示例来源:origin: ysc/HtmlExtractor
@Override
public String fetch(String url) {
try {
LOGGER.debug("url:"+url);
String host = new URL(url).getHost();
Connection conn = Jsoup.connect(url)
.timeout(60000)
.header("Accept", ACCEPT)
.header("Accept-Encoding", ENCODING)
.header("Accept-Language", LANGUAGE)
.header("Connection", CONNECTION)
.header("Referer", "http://"+host)
.header("Host", host)
.header("User-Agent", USER_AGENT)
.ignoreContentType(true);
String html = conn.get().html();
LOGGER.debug("html:"+html);
return html;
}catch (Exception e){
LOGGER.error("获取URL:"+url+"页面出错", e);
}
return "";
}
代码示例来源:origin: org.apache.karaf.cave.server/org.apache.karaf.cave.server.storage
/**
* Returns the given <code>Connection</code> object
* modified by setting the <code>HttpHeaders.AUTHORIZATION</code> header
* depending on whether or not the authorization keys have been set.
*
* @param connection an instance of <code>Connection</code>.
* @return a modified <code>Connection</code> object.
*/
public Connection authorize(Connection connection) {
if (containsAuthorizationKeys()) {
connection.header(HttpHeaders.AUTHORIZATION, getAuthorizationHeader());
}
return connection;
}
代码示例来源:origin: bluetata/crawler-jsoup-maven
public static Document getRemoteURL(String remoteUrl) {
Document doc = null;
int temp = 0;
try {
temp = Integer.parseInt(Math.round(Math.random()*(UserAgent.length-1))+"");
Connection conn = Jsoup.connect(remoteUrl);
conn.header("User-Agent", UserAgent[temp]);
conn.cookie("auth", "token");
doc = conn.timeout(MAX_CONNECT_TIME).get();
} catch (Exception ex) {
logger.error("GrabUnits.class_getRemoteURL 出现异常...");
logger.error(ex);
}
try {
int sleeptime = Integer.parseInt(Math.round(Math.random()*10000)+"");
logger.info(BrowserType[temp] +" GET请求 " + remoteUrl + " 停留 " + sleeptime + " 毫秒。");
Thread.sleep(sleeptime);
} catch (InterruptedException ex) {
logger.error("GrabUnits.class_getRemoteURL 休眠出现异常...");
logger.error(ex);
}
return doc;
}
代码示例来源:origin: bluetata/crawler-jsoup-maven
/***
* 远程访问URL返回Doc
* @param remoteUrl
* @return
*/
public static Document postRemoteURL(String remoteUrl) {
Document doc = null;
int temp = 0;
try {
temp = Integer.parseInt(Math.round(Math.random()*(UserAgent.length-1))+"");
Connection conn = Jsoup.connect(remoteUrl);
conn.header("User-Agent", UserAgent[temp]);
conn.cookie("auth", "token");
doc = conn.timeout(MAX_CONNECT_TIME).post();
} catch (Exception ex) {
logger.error("GrabUnits.class_postRemoteURL 出现异常...");
logger.error(ex);
}
try {
int sleeptime = Integer.parseInt(Math.round(Math.random()*10000)+"");
logger.info(BrowserType[temp] +" POST请求 " + remoteUrl + " 停留 " + sleeptime + " 毫秒。");
Thread.sleep(sleeptime);
} catch (InterruptedException ex) {
logger.error("GrabUnits.class_postRemoteURL 休眠出现异常...");
logger.error(ex);
}
return doc;
}
代码示例来源:origin: openwide-java/artifact-listener
@Override
public List<ArtifactVersionBean> getArtifactVersions(String groupId, String artifactId) throws ServiceException {
String url = String.format(configurer.getArtifactRepositoryMetadataUrl(), groupId.replace(".", "/"), artifactId);
Document doc;
try {
doc = Jsoup.connect(url).header(HTTP.CONN_DIRECTIVE, HTTP.CONN_CLOSE).get();
} catch (IOException e) {
throw new ServiceException("IOException: " + e.getMessage(), e);
}
return parseMavenMetadata(doc);
}
代码示例来源:origin: malmstein/yahnac
private static Connection defaultConnection(String baseUrlExtension) {
Connection conn = Jsoup.connect(BASE_URL + baseUrlExtension)
.timeout(TIMEOUT_MILLIS)
.userAgent(USER_AGENT);
conn.header("Accept-Encoding", "gzip");
return conn;
}
代码示例来源:origin: YuanKJ-/JsCrawler
@Override
protected void processBody(RequestModel model) {
if(model.getBody() != null) {
connection.method(Connection.Method.POST);
connection.header("Content-Type", "application/json");
connection.requestBody(model.getBody());
}
}
代码示例来源:origin: denghuichao/proxy-pool
public String nextPage() {
String html = "";
String url = pageUrl();
pageIndex++;
logger.info("fetching page: " + url);
try {
Connection connection = Jsoup.connect(url);
for (String[] head : HEADERS) {
connection.header(head[0], head[1]);
}
connection.timeout(4000).followRedirects(true);
html = connection.execute().parse().html();//执行
} catch (IOException e) {
logger.info("fetch page error: " + e.getMessage());
}
return html;
}
代码示例来源:origin: delthas/JavaSkype
private Response sendRequest(Method method, String apiPath, boolean absoluteApiPath, String... keyval) throws IOException {
String url = absoluteApiPath ? apiPath : SERVER_HOSTNAME + apiPath;
Connection conn = Jsoup.connect(url).maxBodySize(100 * 1024 * 1024).timeout(10000).method(method).ignoreContentType(true).ignoreHttpErrors(true);
logger.finest("Sending " + method + " request at " + url);
if (skypeToken != null) {
conn.header("X-Skypetoken", skypeToken);
} else {
logger.fine("No token sent for the request at: " + url);
}
conn.data(keyval);
return conn.execute();
}
代码示例来源:origin: indywidualny/FaceSlim
.userAgent(userAgent)
.proxy(Miscellany.getProxy(preferences))
.header("Accept-Encoding", "gzip, deflate")
.timeout(5000)
.cookie("https://m.facebook.com", CookieManager.getInstance().getCookie("https://m.facebook.com"))
代码示例来源:origin: dimtion/Shaarlier
/**
* Helper method which create a new connection to Shaarli
* @param url the url of the shaarli
* @param isPost true if we create a POST request, false for a GET request
* @return pre-made jsoupConnection
*/
private Connection createShaarliConnection(String url, boolean isPost){
Connection jsoupConnection = Jsoup.connect(url);
Connection.Method connectionMethod = isPost ? Connection.Method.POST : Connection.Method.GET;
if (!"".equals(this.mBasicAuth)) {
jsoupConnection = jsoupConnection.header("Authorization", "Basic " + this.mBasicAuth);
}
if (this.mCookies != null){
jsoupConnection = jsoupConnection.cookies(this.mCookies);
}
return jsoupConnection
.validateTLSCertificates(this.mValidateCert)
.timeout(this.mTimeout)
.followRedirects(true)
.method(connectionMethod);
}
代码示例来源:origin: malmstein/yahnac
public Connection loginConnection(String username, String password) {
Connection login = connection(LOGIN_BASE_URL);
return login
.data("go_to", "news")
.data("acct", username)
.data("pw", password)
.header("Origin", ConnectionProvider.BASE_URL)
.followRedirects(true)
.referrer(ConnectionProvider.BASE_URL + ConnectionProvider.LOGIN_URL_EXTENSION)
.method(Connection.Method.POST);
}
内容来源于网络,如有侵权,请联系作者删除!