【java基础】-【api】HttpClient的Post和Get

HttpClient的Post和Get

  • .fluent.Request
    • .Get()
    • .Post()

.fluent.Request

.Get()

     //访问http://httpbin.org/get网址,把结果保存在text中
     String text=Request.Get("http://httpbin.org/get")  //注意此处没有分号;结尾
     
     //将结果返回给test
             .execute().returnContent().toString();       
     System.out.println(text); 

.Post()

    //定义body中的参数
    String data="username:vip,password:secret";
    StringEntity se = new StringEntity(data);
    
    //给接口http://httpbin.org/post发送post请求
    String result= Request.Post("http://httpbin.org/post")
    
    //设置Header的内容
    //	       .addHeader("Host", "my.worktile.com")
    //	       .addHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:47.0) Gecko/20100101 Firefox/47.0")
    //	       .addHeader("Accept", "application/json, text/plain, */*")
    //	       .addHeader("Accept-Language", "zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3")
    //	       .addHeader("Accept-Encoding", "gzip, deflate, br")
    //	       .addHeader("Content-Type", "application/json;charset=utf-8")
    //	       .addHeader("Connection", "keep-alive")
    
    //发送body数据
           .body(se)
    //将结果返回给result
           .execute().returnContent().asString();

    System.out.println(result);

你可能感兴趣的:(java,java)