<!-- https://mvnrepository.com/artifact/com.arronlong/httpclientutil --> <dependency> <groupId>com.arronlong</groupId> <artifactId>httpclientutil</artifactId> <version>1.0.4</version> </dependency>
简单Demo在test包里还有各种测试demo,各测试类的源码在src/test/java/com/httpclient/test包路径下。
1 2 3 4 public static void main(String[] args) throws HttpProcessException, FileNotFoundException { 5 String url = "https://github.com/Arronlong/httpclientutil"; 6 7 //最简单的使用: 8 String html = HttpClientUtil.get(HttpConfig.custom().url(url)); 9 System.out.println(html); 10 11 //--------------------------------- 12 // 【详细说明】 13 //-------------------------------- 14 15 //插件式配置Header(各种header信息、自定义header) 16 Header[] headers = HttpHeader.custom() 17 .userAgent("javacl") 18 .other("customer", "自定义") 19 .build(); 20 21 //插件式配置生成HttpClient时所需参数(超时、连接池、ssl、重试) 22 HCB hcb = HCB.custom() 23 .timeout(1000) //超时 24 .pool(100, 10) //启用连接池,每个路由最大创建10个链接,总连接数限制为100个 25 .sslpv(SSLProtocolVersion.TLSv1_2) //设置ssl版本号,默认SSLv3,也可以调用sslpv("TLSv1.2") 26 .ssl() //https,支持自定义ssl证书路径和密码,ssl(String keyStorePath, String keyStorepass) 27 .retry(5) //重试5次 28 ; 29 30 HttpClient client = hcb.build(); 31 32 Map<String, Object> map = new HashMap<String, Object>(); 33 map.put("key1", "value1"); 34 map.put("key2", "value2"); 35 36 //插件式配置请求参数(网址、请求参数、编码、client) 37 HttpConfig config = HttpConfig.custom() 38 .headers(headers) //设置headers,不需要时则无需设置 39 .url(url) //设置请求的url 40 .map(map) //设置请求参数,没有则无需设置 41 .encoding("utf-8") //设置请求和返回编码,默认就是Charset.defaultCharset() 42 .client(client) //如果只是简单使用,无需设置,会自动获取默认的一个client对象 43 //.inenc("utf-8") //设置请求编码,如果请求返回一直,不需要再单独设置 44 //.inenc("utf-8") //设置返回编码,如果请求返回一直,不需要再单独设置 45 //.json("json字符串") //json方式请求的话,就不用设置map方法,当然二者可以共用。 46 //.context(HttpCookies.custom().getContext()) //设置cookie,用于完成携带cookie的操作 47 //.out(new FileOutputStream("保存地址")) //下载的话,设置这个方法,否则不要设置 48 //.files(new String[]{"d:/1.txt","d:/2.txt"}) //上传的话,传递文件路径,一般还需map配置,设置服务器保存路径 49 ; 50 51 52 //使用方式: 53 String result1 = HttpClientUtil.get(config); //get请求 54 String result2 = HttpClientUtil.post(config); //post请求 55 System.out.println(result1); 56 System.out.println(result2); 57 58 //HttpClientUtil.down(config); //下载,需要调用config.out(fileOutputStream对象) 59 //HttpClientUtil.upload(config); //上传,需要调用config.files(文件路径数组) 60 61 //如果指向看是否访问正常 62 //String result3 = HttpClientUtil.head(config); // 返回Http协议号+状态码 63 //int statusCode = HttpClientUtil.status(config);//返回状态码 64 65 //[新增方法]sendAndGetResp,可以返回原生的HttpResponse对象, 66 //同时返回常用的几类对象:result、header、StatusLine、StatusCode 67 HttpResult respResult = HttpClientUtil.sendAndGetResp(config); 68 System.out.println("返回结果:\n"+respResult.getResult()); 69 System.out.println("返回resp-header:"+respResult.getRespHeaders());//可以遍历 70 System.out.println("返回具体resp-header:"+respResult.getHeaders("Date")); 71 System.out.println("返回StatusLine对象:"+respResult.getStatusLine()); 72 System.out.println("返回StatusCode:"+respResult.getStatusCode()); 73 System.out.println("返回HttpResponse对象)(可自行处理):"+respResult.getResp()); 74 }