當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。