jsoup请求http或https返回json字符串工具类
json
适用于现代 C++ 的 JSON。
项目地址:https://gitcode.com/gh_mirrors/js/json
免费下载资源
·
jsoup官网:https://jsoup.org/
require lib: jsoup-1.9.2.jar
jsoup返回json字符串工具类JsoupHelper.java:
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.jsoup.Connection;
import org.jsoup.Connection.Response;
import org.jsoup.Jsoup;
/**
* @author zhuxiongxian
* @version 1.0
* @created at 2017年3月17日 下午6:06:32
*/
public class JsoupHelper {
/**
* 获取连接
*
* @param url
* 请求url
* @param params
* 参数
* @param charset
* 参数编码方式
* @param headers
* 头部信息
* @return
*/
public static Connection getConnection(String url, Map<String, String> params, String charset,
Map<String, String> headers) {
if (params != null) {
List<NameValuePair> paramList = new ArrayList<NameValuePair>();
Iterator<Entry<String, String>> iter = params.entrySet().iterator();
while (iter.hasNext()) {
Entry<String, String> entry = iter.next();
String key = entry.getKey();
String value = entry.getValue();
paramList.add(new BasicNameValuePair(key, value));
}
try {
String paramStr = EntityUtils.toString(new UrlEncodedFormEntity(paramList, charset));
StringBuffer sb = new StringBuffer();
sb.append(url);
if (url.indexOf("?") > 0) {
sb.append("&");
} else {
sb.append("?");
}
sb.append(paramStr);
url = sb.toString();
} catch (Exception e) {
e.printStackTrace();
}
}
Connection conn = Jsoup.connect(url);
conn.timeout(10000); // 10秒超时
conn.ignoreContentType(true);
if (headers != null) {
Iterator<Entry<String, String>> iter = headers.entrySet().iterator();
while (iter.hasNext()) {
Entry<String, String> entry = iter.next();
String key = entry.getKey();
String value = entry.getValue();
conn.header(key, value);
}
}
return conn;
}
public static String get(String url, Map<String, String> params, String charset,
Map<String, String> headers) {
String result = "";
try {
Connection conn = getConnection(url, params, charset, headers);
Response response = conn.execute();
result = response.body();
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
}
用法:
import java.util.HashMap;
import java.util.Map;
/**
* @author zhuxiongxian
* @version 1.0
* @created at 2017年4月5日 下午4:43:22
*/
public class JsoupTest {
public static Map<String, String> headers = null;
static {
headers = new HashMap<String, String>();
headers.put("User-Agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.110 Safari/537.36");
headers.put("Accept-Language", "zh-CN,zh;q=0.8");
headers.put("Accept-Encoding", "gzip, deflate, sdch, br");
headers.put("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
headers.put("Connection", "Keep-Alive");
headers.put("Content-Type", "application/json;charset=UTF-8");
}
public void test() {
String url = "http://xxxxxx";
String content = JsoupHelper.get(url, null, "utf-8", headers);
System.out.println("返回的json字符串:" + content);
}
}
GitHub 加速计划 / js / json
41.72 K
6.61 K
下载
适用于现代 C++ 的 JSON。
最近提交(Master分支:1 个月前 )
960b763e
3 个月前
8c391e04
6 个月前
更多推荐
已为社区贡献4条内容
所有评论(0)