2626public class Demo {
2727
2828 public static void main (String [] args ) throws HttpProcessException , FileNotFoundException {
29- String url = "https ://github.com/Arronlong/httpclientUtil " ;
29+ String url = "http ://localhost:8080/log/getTest " ;
3030
3131 //最简单的使用:
32- String html = HttpClientUtil .get (HttpConfig .custom ().url (url ));
32+ /* String html = HttpClientUtil.get(HttpConfig.custom()./service/http://github.com/url(url));
3333 System.out.println(html);
34-
34+ */
3535 //---------------------------------
3636 // 【详细说明】
3737 //--------------------------------
@@ -40,6 +40,7 @@ public static void main(String[] args) throws HttpProcessException, FileNotFound
4040 Header [] headers = HttpHeader .custom ()
4141 .userAgent ("javacl" )
4242 .other ("customer" , "自定义" )
43+ .contentType ("application/json" )
4344 .build ();
4445
4546 //插件式配置生成HttpClient时所需参数(超时、连接池、ssl、重试)
@@ -54,19 +55,18 @@ public static void main(String[] args) throws HttpProcessException, FileNotFound
5455 HttpClient client = hcb .build ();
5556
5657 Map <String , Object > map = new HashMap <String , Object >();
57- map .put ("key1" , "value1" );
58- map .put ("key2" , "value2" );
58+ map .put ("username" , "value1" );
5959
6060 //插件式配置请求参数(网址、请求参数、编码、client)
6161 HttpConfig config = HttpConfig .custom ()
6262 .headers (headers ) //设置headers,不需要时则无需设置
6363 .url (url ) //设置请求的url
64- .map (map ) //设置请求参数,没有则无需设置
64+ // .map(map) //设置请求参数,没有则无需设置
6565 .encoding ("utf-8" ) //设置请求和返回编码,默认就是Charset.defaultCharset()
6666 .client (client ) //如果只是简单使用,无需设置,会自动获取默认的一个client对象
6767 //.inenc("utf-8") //设置请求编码,如果请求返回一直,不需要再单独设置
6868 //.inenc("utf-8") //设置返回编码,如果请求返回一直,不需要再单独设置
69- // .json("json字符串 ") //json方式请求的话,就不用设置map方法,当然二者可以共用。
69+ .json ("RTCCNdJYI0h0R49TkRVdOx39sv6tlYXlFPKNc6VHzkc= " ) //json方式请求的话,就不用设置map方法,当然二者可以共用。
7070 //.context(HttpCookies.custom().getContext()) //设置cookie,用于完成携带cookie的操作
7171 //.out(new FileOutputStream("保存地址")) //下载的话,设置这个方法,否则不要设置
7272 //.files(new String[]{"d:/1.txt","d:/2.txt"}) //上传的话,传递文件路径,一般还需map配置,设置服务器保存路径
@@ -75,9 +75,9 @@ public static void main(String[] args) throws HttpProcessException, FileNotFound
7575
7676 //使用方式:
7777 String result1 = HttpClientUtil .get (config ); //get请求
78- String result2 = HttpClientUtil .post (config ); //post请求
78+ // String result2 = HttpClientUtil.post(config); //post请求
7979 System .out .println (result1 );
80- System .out .println (result2 );
80+ // System.out.println(result2);
8181
8282 //HttpClientUtil.down(config); //下载,需要调用config.out(fileOutputStream对象)
8383 //HttpClientUtil.upload(config); //上传,需要调用config.files(文件路径数组)
0 commit comments