Java Code Examples for org.jsoup.Connection#method()

The following examples show how to use org.jsoup.Connection#method() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HttpUtil.java    From JsDroidCmd with Mozilla Public License 2.0 6 votes vote down vote up
public static String post(String url, Map<String, String> headers,
		Map<String, String> params) {
	try {
		Connection connect = Jsoup.connect(url);
		connect.ignoreContentType(true);
		connect.ignoreHttpErrors(true);
		if (params != null) {
			connect.data(params);
		}
		if (headers != null) {
			connect.headers(headers);
		}
		connect.method(Method.POST);
		return connect.execute().body();
	} catch (Throwable e) {
	}
	return null;
}
 
Example 2
Source File: PostComment.java    From WordPressHelper with MIT License 6 votes vote down vote up
@Override
protected Object doInBackground(Object[] params) {
    Connection connection  = Jsoup.connect(URL_WORDPRESS + "/wp-comments-post.php");
    connection.method(Connection.Method.POST);
    connection.data("author", name);
    connection.data("email", email);
    connection.data("url", url);
    connection.data("comment", comment);
    connection.data("comment_post_ID", id);
    connection.data("comment_parent", commentParent);
    try {
        response = connection.execute().statusCode();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}
 
Example 3
Source File: JsoupUtil.java    From xxl-crawler with GNU General Public License v3.0 4 votes vote down vote up
public static String loadPageSource(PageRequest pageRequest) {
    if (!UrlUtil.isUrl(pageRequest.getUrl())) {
        return null;
    }
    try {
        // 请求设置
        Connection conn = Jsoup.connect(pageRequest.getUrl());
        if (pageRequest.getParamMap() != null && !pageRequest.getParamMap().isEmpty()) {
            conn.data(pageRequest.getParamMap());
        }
        if (pageRequest.getCookieMap() != null && !pageRequest.getCookieMap().isEmpty()) {
            conn.cookies(pageRequest.getCookieMap());
        }
        if (pageRequest.getHeaderMap()!=null && !pageRequest.getHeaderMap().isEmpty()) {
            conn.headers(pageRequest.getHeaderMap());
        }
        if (pageRequest.getUserAgent()!=null) {
            conn.userAgent(pageRequest.getUserAgent());
        }
        if (pageRequest.getReferrer() != null) {
            conn.referrer(pageRequest.getReferrer());
        }
        conn.timeout(pageRequest.getTimeoutMillis());
        conn.validateTLSCertificates(pageRequest.isValidateTLSCertificates());
        conn.maxBodySize(0);    // 取消默认1M限制

        // 代理
        if (pageRequest.getProxy() != null) {
            conn.proxy(pageRequest.getProxy());
        }

        conn.ignoreContentType(true);
        conn.method(pageRequest.isIfPost()?Connection.Method.POST:Connection.Method.GET);

        // 发出请求
        Connection.Response resp = conn.execute();
        String pageSource = resp.body();
        return pageSource;
    } catch (IOException e) {
        logger.error(e.getMessage(), e);
        return null;
    }
}
 
Example 4
Source File: HttpConnectionTest.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
@Test public void method() {
    Connection con = HttpConnection.connect("http://example.com/");
    assertEquals(Connection.Method.GET, con.request().method());
    con.method(Connection.Method.POST);
    assertEquals(Connection.Method.POST, con.request().method());
}
 
Example 5
Source File: HttpConnectionTest.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
@Test public void method() {
    Connection con = HttpConnection.connect("http://example.com/");
    assertEquals(Connection.Method.GET, con.request().method());
    con.method(Connection.Method.POST);
    assertEquals(Connection.Method.POST, con.request().method());
}
 
Example 6
Source File: HttpConnectionTest.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
@Test public void method() {
    Connection con = HttpConnection.connect("http://example.com/");
    assertEquals(Connection.Method.GET, con.request().method());
    con.method(Connection.Method.POST);
    assertEquals(Connection.Method.POST, con.request().method());
}
 
Example 7
Source File: HttpConnectionTest.java    From jsoup-learning with MIT License 4 votes vote down vote up
@Test public void method() {
    Connection con = HttpConnection.connect("http://example.com/");
    assertEquals(Connection.Method.GET, con.request().method());
    con.method(Connection.Method.POST);
    assertEquals(Connection.Method.POST, con.request().method());
}