java httpclient Post

一、Maven引用httpclient


  org.apache.httpcomponents
  httpclient
  4.5.3

二、直接上代码

 private  void  PostData(){
        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpPost httpPost = new HttpPost("https://www.baidu.com/");
        httpPost.setHeader("Content-type", "application/json;charset=utf-8");
        httpPost.setHeader("Connection", "Close");
        httpPost.setHeader("Authorization","Basic xxxxxxxx=");
        String jsonStr="{}";
        StringEntity entity = new StringEntity(jsonStr, Charset.forName("UTF-8"));
        entity.setContentEncoding("UTF-8");
        entity.setContentType("application/json");
        httpPost.setEntity(entity);
        CloseableHttpResponse httpResponse;
        String result = null;
        try {
            httpResponse = httpClient.execute(httpPost);
            result = EntityUtils.toString(httpResponse.getEntity(), "UTF-8");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

运行效果: 

java httpclient Post_第1张图片

你可能感兴趣的:(Java,java,python,开发语言)