当前位置: 首页>>代码示例>>Java>>正文


Java Connection.header方法代码示例

本文整理汇总了Java中org.jsoup.Connection.header方法的典型用法代码示例。如果您正苦于以下问题:Java Connection.header方法的具体用法?Java Connection.header怎么用?Java Connection.header使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在org.jsoup.Connection的用法示例。


在下文中一共展示了Connection.header方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: getConnect

import org.jsoup.Connection; //导入方法依赖的package包/类
public static Connection getConnect(String url, Map<String,String> header, Map<String,String> cookie){
		Connection con;
//		Document doc = null;
		url = url.trim();
		con = Jsoup.connect(url);
		con.ignoreContentType(true);
//		if(!url.startsWith("http:/")){
//			return null;
//		}
		if(url.split(":", 2)[0].equals("https")){
			con.validateTLSCertificates(false);
		}
		if(!(header==null||header.isEmpty())){
			for(Object key : header.keySet()){
				con.header((String) key, header.get(key));
			}
		}else{
			con.header("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
		}
		if(!(cookie==null||cookie.isEmpty()))
			con.cookies(cookie);
//		try {
//			doc = con.get();
//		} catch (IOException e) {
//			// TODO Auto-generated catch block
//		e.printStackTrace();logger.error("Exception",e);
//		}
		con.ignoreHttpErrors(true).ignoreContentType(true);
		return con;
	}
 
开发者ID:zrtzrt,项目名称:CrawlerSYS,代码行数:31,代码来源:WebCrawler.java

示例2: getGroupsList

import org.jsoup.Connection; //导入方法依赖的package包/类
@Override
public ArrayList<Group> getGroupsList(Component c, Context context, String id, String pwd) throws IOException {
    ArrayList<Group> liste = new ArrayList<>();
    String nameComponent = String.copyValueOf(c.name.toCharArray());

    String url = c.groups_url;
    Connection conn = Jsoup.connect(url);
    String login = id+":"+pwd;
    String b64login = new String(android.util.Base64.encode(login.getBytes(), android.util.Base64.DEFAULT));
    conn.header("Authorization", "Basic " + b64login);

    Connection.Response resp = conn.execute();
    if(resp.statusCode() == 200){
        if(AuthManager.needAccount(nameComponent, context)) {
            AuthManager.addAccount(id, pwd, nameComponent, context);
        }
    }

    Document doc = conn.get();
    for (Element e : doc.select("option[value$=.html]"))
    {
        Group groupe = new Group();
        groupe.name = e.text();
        groupe.dataSourceType = DataSourceType.CELCAT;
        groupe.dataSource = getRefactoredUrl(url)+(e.attr("value").replaceAll(".html", ".xml"));

        groupe.component = c;
        liste.add(groupe);
    }
    return liste;
}
 
开发者ID:natinusala,项目名称:openedt,代码行数:32,代码来源:CelcatAdapter.java

示例3: getConnection

import org.jsoup.Connection; //导入方法依赖的package包/类
/**
 * 构建一个同步抓取连接
 */
public static Connection getConnection(String url, String cookie, String UA, String referer, String host) {
    Connection connection = Jsoup.connect(url).timeout(60000).ignoreContentType(true).ignoreHttpErrors(true);
    connection.header("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
    connection.header("Accept-Encoding", "gzip,deflate,sdch");
    connection.header("Accept-Language", "zh-CN,zh;q=0.8,en;q=0.6,zh-TW;q=0.4");
    connection.header("Cache-Control", "no-cache");
    connection.header("Connection", "keep-alive");
    if (!StringUtils.isEmpty(UA)) {
        connection.header("User-Agent", UA);
    }
    if (!StringUtils.isEmpty(referer)) {
        connection.header("Referer", referer);
    }
    if (!StringUtils.isEmpty(cookie)) {
        connection.header("Cookie", cookie);
    }

    if (StringUtils.isEmpty(host)) {
        host = HtmlUtils.getServerHost(url);
    }
    if (StringUtils.isNotEmpty(host)) {
        connection.header("Host", host);
    }

    return connection;
}
 
开发者ID:MartinDai,项目名称:TBSpider,代码行数:30,代码来源:HttpUtils.java

示例4: sendRequest

import org.jsoup.Connection; //导入方法依赖的package包/类
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();
}
 
开发者ID:delthas,项目名称:JavaSkype,代码行数:13,代码来源:WebConnector.java

示例5: simulateLogin

import org.jsoup.Connection; //导入方法依赖的package包/类
/**
 * @param userName 用户名
 * @param pwd 密码
 * @throws Exception
 */
public static void simulateLogin(String userName, String pwd) throws Exception {

    /* 
     * 第一次请求 
     * grab login form page first
     * 获取登陆提交的表单信息,及修改其提交data数据(login,password)
     */
    // get the response, which we will post to the action URL(rs.cookies())
    Connection con = Jsoup.connect(LOGIN_URL);  // 获取connection
    con.header(USER_AGENT, USER_AGENT_VALUE);   // 配置模拟浏览器
    Response rs = con.execute();                // 获取响应
    Document d1 = Jsoup.parse(rs.body());       // 转换为Dom树
    List<Element> eleList = d1.select("form");  // 获取提交form表单,可以通过查看页面源码代码得知

    // 获取cooking和表单属性
    // lets make data map containing all the parameters and its values found in the form
    Map<String, String> datas = new HashMap<>();
    for (Element e : eleList.get(0).getAllElements()) {
        // 设置用户名
        if (e.attr("name").equals("login")) {
            e.attr("value", userName);
        }
        // 设置用户密码
        if (e.attr("name").equals("password")) {
            e.attr("value", pwd);
        }
        // 排除空值表单属性
        if (e.attr("name").length() > 0) {
            datas.put(e.attr("name"), e.attr("value"));
        }
    }

    /*
     * 第二次请求,以post方式提交表单数据以及cookie信息
     */
    Connection con2 = Jsoup.connect("https://github.com/session");
    con2.header(USER_AGENT, USER_AGENT_VALUE);
    // 设置cookie和post上面的map数据
    Response login = con2.ignoreContentType(true).followRedirects(true).method(Method.POST).data(datas).cookies(rs.cookies()).execute();
    // 打印,登陆成功后的信息
    System.out.println(login.body());

    // 登陆成功后的cookie信息,可以保存到本地,以后登陆时,只需一次登陆即可
    Map<String, String> map = login.cookies();
    for (String s : map.keySet()) {
        System.out.println(s + " : " + map.get(s));
    }
}
 
开发者ID:bluetata,项目名称:crawler-jsoup-maven,代码行数:54,代码来源:GITHUBLoginApater.java

示例6: getData

import org.jsoup.Connection; //导入方法依赖的package包/类
/**
 * Make the query to google and return the data.
 *
 * @param query
 *            textfield for google
 * @return webpage in Document format
 */
private Document getData(String query) throws CaptchaException, EmptyQueryException, UnsupportedEncodingException {
    if (this.query.isEmpty() || this.query == null) {
        throw new EmptyQueryException();
    }

    Connection conn = null;
    Document doc = null;

    String request = "https://www.google.com/search?q=" + URLEncoder.encode( stripXSS(query), "UTF-8");
    if(!tokenCookie.isEmpty()){
        request = request + "&google_abuse=" + URLEncoder.encode(tokenCookie, "UTF-8");
    }

    try {
        conn = Jsoup
                .connect(request)
                .method(Method.GET)
                .userAgent("Mozilla/5.0 (Windows NT 6.3; WOW64; rv:36.0) Gecko/20100101 Firefox/48.0")
                .header("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8")
                .header("Cookie", tokenCookie)
                .header("Connection", "keep-alive")
                .ignoreHttpErrors(true)
                .timeout(5000);

        if(!referer.isEmpty()){
            conn.header("Referer", referer);
        }

        Connection.Response response = conn.execute();

        if (response.statusCode() == 503) {

            referer = response.url().toString();
            idCaptcha = getIDCaptcha(response.parse());

            getCaptcha("https://ipv4.google.com/sorry/image?id=" + idCaptcha + "&hl=es&" + referer.substring(referer.indexOf('?')+1));

            throw new CaptchaException();

        }

        doc = Jsoup.parse(response.body());

        // Clean the response
        Whitelist wl = new Whitelist().basic();
        wl.addAttributes("span", "class");
        Cleaner clean = new Cleaner(wl);
        doc = clean.clean(doc);
    } catch (IOException e) {
        //System.out.println(e.getMessage());
        e.printStackTrace();
    }

    return doc;
}
 
开发者ID:ElevenPaths,项目名称:GoogleIndexRetriever,代码行数:63,代码来源:GoogleSearch.java

示例7: createStringConnection

import org.jsoup.Connection; //导入方法依赖的package包/类
/**
 * 构建一个返回类型为String的抓取连接
 */
public static Connection createStringConnection(boolean isUseProxy, String url, String cookie, String UA, String referer, String host) {
    Connection connection = getConnection(url, cookie, UA, referer, host);
    connection.header("Accept", "*/*");
    return connection.ignoreContentType(true);
}
 
开发者ID:MartinDai,项目名称:TBSpider,代码行数:9,代码来源:HttpUtils.java


注:本文中的org.jsoup.Connection.header方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。