本文整理汇总了Java中org.jsoup.Connection.userAgent方法的典型用法代码示例。如果您正苦于以下问题:Java Connection.userAgent方法的具体用法?Java Connection.userAgent怎么用?Java Connection.userAgent使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.jsoup.Connection
的用法示例。
在下文中一共展示了Connection.userAgent方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: saveToFile
import org.jsoup.Connection; //导入方法依赖的package包/类
public static File saveToFile(String address, String localFileName) throws IOException {
File file = new File(localFileName);
Connection connection = Jsoup.connect(address).ignoreContentType(true).ignoreHttpErrors(true);
connection.userAgent("Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36");
connection.timeout(10000);
Connection.Response resultImageResponse = connection.execute();
FileOutputStream out = (new FileOutputStream(file));
out.write(resultImageResponse.bodyAsBytes()); // resultImageResponse.body() is where the image's contents are.
out.close();
return file;
}
示例2: loadParse
import org.jsoup.Connection; //导入方法依赖的package包/类
/**
* 按照规则,加载解析html
* @param url :加载URL
* @param paramMap :请求参数
* @param cookieMap :请求cookie
* @param ifPost :是否使用post请求
* @param tagMap :解析规则[0-选择器方式、1-id方式、2-class方式、3-tag]
* @return
*/
public static Elements loadParse(String url, Map<String, String> paramMap, Map<String, String> cookieMap,
boolean ifPost, Map<Integer, Set<String>> tagMap) {
if (!isUrl(url)) {
return null;
}
try {
// 请求设置
Connection conn = Jsoup.connect(url);
conn.userAgent("Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36");
if (paramMap != null && !paramMap.isEmpty()) {
conn.data(paramMap);
}
if (cookieMap != null && !cookieMap.isEmpty()) {
conn.cookies(cookieMap);
}
conn.timeout(5000);
// 发出请求
Document html = null;
if (ifPost) {
html = conn.post();
} else {
html = conn.get();
}
// 过滤元素
Elements resultAll = new Elements();
if (tagMap != null && !tagMap.isEmpty()) {
for (Entry<Integer, Set<String>> tag : tagMap.entrySet()) {
int tagType = tag.getKey();
Set<String> tagNameList = tag.getValue();
if (tagNameList != null && tagNameList.size() > 0) {
for (String tagName : tagNameList) {
if (tagType == 0) {
Elements resultSelect = html.select(tagName); // 选择器方式
resultAll.addAll(resultSelect);
} else if (tagType == 1) {
Element resultId = html.getElementById(tagName); // 元素ID方式
resultAll.add(resultId);
} else if (tagType == 2) {
Elements resultClass = html.getElementsByClass(tagName); // ClassName方式
resultAll.addAll(resultClass);
} else if (tagType == 3) {
Elements resultTag = html.getElementsByTag(tagName); // html标签方式
resultAll.addAll(resultTag);
}
}
}
}
} else {
resultAll = html.getElementsByTag("body");
}
return resultAll;
} catch (IOException e) {
logger.error("", e);
}
return null;
}
示例3: postJspToHtml
import org.jsoup.Connection; //导入方法依赖的package包/类
public void postJspToHtml(String postUrl, String filePath,String fileName) throws Exception{
HttpServletRequest request=Struts2Utils.getRequest();
//${pageContext.request.scheme}://${pageContext.request.serverName }:${pageContext.request.serverPort} pageContext.request.contextPath
String reqTarget = request.getScheme()+"://"+request.getServerName()+(request.getServerPort()==80?"":":"+request.getServerPort())+request.getContextPath();
reqTarget =reqTarget+"/toHtml";
//?url="+postUrl+"&filePath="+filePath+"&fileName="+fileName;
Map<String, String> map=new HashMap<String, String>();
map.put("url", postUrl);
map.put("filePath", filePath);
map.put("fileName", fileName);
Connection connection = Jsoup.connect(reqTarget);
connection.userAgent("Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.64 Safari/537.31");
connection.data(map);
Document doc=connection.timeout(8000).get();
}
示例4: 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;
}
}
示例5: userAgent
import org.jsoup.Connection; //导入方法依赖的package包/类
@Test public void userAgent() {
Connection con = HttpConnection.connect("http://example.com/");
con.userAgent("Mozilla");
assertEquals("Mozilla", con.request().header("User-Agent"));
}