JAvaデータ(HttpClientUtil)
22102 ワード
HttpClientUtil使用(コード1を参照)コード1:
タスク:ページの内容をスクロールし、ターゲットページはjsだけでデータを取得できるので、アクセスインタフェースでデータを取得するだけです.
public static void main(String[] args) throws HttpProcessException, FileNotFoundException {
String url = "https://github.com/Arronlong/httpclientutil";
// :
String html = HttpClientUtil.get(HttpConfig.custom().url(url));
System.out.println(html);
//---------------------------------
// 【 】
//--------------------------------
// Header( header 、 header)
Header[] headers = HttpHeader.custom()
.userAgent("javacl")
.other("customer", " ")
.build();
// HttpClient ( 、 、ssl、 )
HCB hcb = HCB.custom()
.timeout(1000) //
.pool(100, 10) // , 10 , 100
.sslpv(SSLProtocolVersion.TLSv1_2) // ssl , SSLv3, sslpv("TLSv1.2")
.ssl() //https, ssl ,ssl(String keyStorePath, String keyStorepass)
.retry(5) // 5
;
HttpClient client = hcb.build();
Map<String, Object> map = new HashMap<String, Object>();
map.put("key1", "value1");
map.put("key2", "value2");
// ( 、 、 、client)
HttpConfig config = HttpConfig.custom()
.headers(headers) // headers,
.url(url) // url
.map(map) // ,
.encoding("utf-8") // , Charset.defaultCharset()
.client(client) // , , client
//.inenc("utf-8") // , ,
//.inenc("utf-8") // , ,
//.json("json ") //json , map , 。
//.context(HttpCookies.custom().getContext()) // cookie, cookie
//.out(new FileOutputStream(" ")) // , ,
//.files(new String[]{"d:/1.txt","d:/2.txt"}) // , , map ,
;
// :
String result1 = HttpClientUtil.get(config); //get
String result2 = HttpClientUtil.post(config); //post
System.out.println(result1);
System.out.println(result2);
//HttpClientUtil.down(config); // , config.out(fileOutputStream )
//HttpClientUtil.upload(config); // , config.files( )
//
//String result3 = HttpClientUtil.head(config); // Http +
//int statusCode = HttpClientUtil.status(config);//
//[ ]sendAndGetResp, HttpResponse ,
// :result、header、StatusLine、StatusCode
HttpResult respResult = HttpClientUtil.sendAndGetResp(config);
System.out.println(" :
"+respResult.getResult());
System.out.println(" resp-header:"+respResult.getRespHeaders());//
System.out.println(" resp-header:"+respResult.getHeaders("Date"));
System.out.println(" StatusLine :"+respResult.getStatusLine());
System.out.println(" StatusCode:"+respResult.getStatusCode());
System.out.println(" HttpResponse )( ):"+respResult.getResp());
}
タスク:ページの内容をスクロールし、ターゲットページはjsだけでデータを取得できるので、アクセスインタフェースでデータを取得するだけです.
private static void getDevice() {
Header[] headers = HttpHeader.custom().contentType("application/json;charset=UTF-8")
.userAgent("Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.97 Safari/537.36")
.other("Referer", "http://39.100.241.89:8888/search_deviceScan/abilityList")
.build();
HttpConfig config = HttpConfig.custom()
.headers(headers) // headers,
.url("http://47.92.80.227:8764/SoftHardware/allType") // url
.json("{\"softHardware\":\"hardware\"}") // ,
.encoding("utf-8"); // , Charset.defaultCharset()
try {
String result = HttpClientUtil.post(config);
JSONObject jsonData = JSONObject.fromObject(result);
JSONArray data = jsonData.getJSONArray("data");
for (int i = 0; i < data.size(); i++) {
JSONObject json = data.getJSONObject(i);
String type = json.getString("key");
JSONArray brandDataArr = json.getJSONArray("data");
for (int j = 0; j < brandDataArr.size(); j++) {
JSONObject brandData = brandDataArr.getJSONObject(j);
getDeviceType(brandData.getString("brand"), type);
}
}
} catch (HttpProcessException e) {
e.printStackTrace();
}
}