码迷,mamicode.com
首页 > 其他好文 > 详细

RestTemplate调用接口(附有账号密码)

时间:2019-05-20 01:24:20      阅读:354      评论:0      收藏:0      [点我收藏+]

标签:build   pre   int   mep   username   dig   bsp   def   system   

private JSONObject Post(String url, String payload, String username, String password) {
        RestTemplate template = new RestTemplate();
        CredentialsProvider provider = new BasicCredentialsProvider();
        UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(username, password);
        provider.setCredentials(AuthScope.ANY, credentials);
        CloseableHttpClient client = HttpClientBuilder.create().setDefaultCredentialsProvider(provider)
                .useSystemProperties().build();
        HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactoryDigestAuth(client);
        template.setRequestFactory(requestFactory);
        JSONObject jsonObject = JSONObject.fromObject(payload);
        System.out.println(JSON.toJSONString(jsonObject));
        JSONObject resultObj = template.postForObject(url, jsonObject, JSONObject.class);return resultObj;
    }

 

RestTemplate调用接口(附有账号密码)

标签:build   pre   int   mep   username   dig   bsp   def   system   

原文地址:https://www.cnblogs.com/chenmz1995/p/10891590.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!